10

我正在尝试使用drupal 7的forms api来创建一个不是“提交”类型的按钮,而是普通的“按钮”类型的按钮,但我似乎无法得到它。

我尝试了很多事情,比如将#type 设置为'button',将#button_type 设置为'button',但无论我做什么,drupal 总是会创建一个“提交”类型的按钮。

4

6 回答 6

12

您可以使用:

"#executes_submit_callback" => FALSE

禁用“提交”步骤。

如果您只想禁用“验证”步骤,请使用:

"#limit_validation_errors" => array()
于 2011-04-26T15:15:16.470 回答
3

在 Drupal 7 中,这可以通过添加:

'#attributes' => array('onclick' => 'return (false);'),

到您的按钮定义。例如:

$form['my_form'] = array(
 '#type' => 'button',
 '#attributes' => array('onclick' => 'return (false);'),
 '#value' => t('My Button'),
 '#prefix' => t('<div class="myButton">'),
 '#suffix' => t('</div>')
);

这适用于我的应用程序。

参考:https ://www.drupal.org/node/283065下禁用和覆盖按钮

于 2015-01-21T16:31:07.877 回答
2

一个非常简单的侧面步骤是您的表单中的以下内容

$form['your-form-element'] = array(
    '#type' => 'button',
    '#name' => 'any-name',
    '#value' => t('Button Text'),
);

在您的表单模板中:

print str_replace('type="submit"', 'type="button"', drupal_render($form['your-form-element']));
于 2012-02-21T14:29:54.947 回答
2

您可能需要查看此问题的一些背景,然后考虑此解决方法。您也许还可以使用#markup手动插入它。

于 2011-01-06T20:38:30.770 回答
1

在模板的 template.php 文件中添加以下函数。

function templatename_button($variables) {
  $element = $variables['element'];
  $type = strtolower($element['#button_type']);
  switch($type){
    case 'submit':
    case 'reset':
    case 'button':
      break;
    default:
      $type = 'submit';
      break;
  }
  $element['#attributes']['type'] = $type;

  element_set_attributes($element, array('id', 'name', 'value'));

  $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  if (!empty($element['#attributes']['disabled'])) {
    $element['#attributes']['class'][] = 'form-button-disabled';
  }

  return '<input' . drupal_attributes($element['#attributes']) . ' />';
}

并以您的形式

  $form['mybutton'] = array(
    '#type'  => 'button',
    '#value' =>  t('mytext'),
    '#button_type' => 'button',
  );
于 2011-08-04T22:55:37.247 回答
0

有时我们需要定义一个默认按钮来提交表单,但是在drupal中所有的按钮元素( #type=button, ),属性总是是,所以必须修改这个属性来指定一个我们需要的默认按钮。submitTYPE"submit""button"

  1. 渲染表单元素并替换 TYPE 属性。

    echo strtr(drupal_render($form['btn']), array('type="submit"' => 'type="button"'));

  2. 修改表单定义。

    form['btn']['#attributes'] = array('onclick' => 'this.type="submit"');

于 2013-04-02T09:44:36.807 回答