如何更改提交按钮类?


12
 <input type="submit" class="form-submit" value="" name="op" id="edit-submit">

我想将类从“ form-submit”更改为“ form-submitone”。

我该怎么办?“表单提交”类从何而来?

Answers:


22

如果您是我,则不会更改类名,而是将自己的类添加到form元素。您可以通过在自定义模块中或在主题中(如果是Drupal 7)实现hook_form_alter来实现。代码看起来像这样:

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id = 'my_form') {
    $form['actions']['submit']['#attributes']['class'][] = 'form-submitone';
  }
}
?>

顺便说一句,提交按钮元素不一定是$form['actions']['submit']。您将必须确定在特定情况下哪个元素是“提交”按钮。您可以通过输出$ form变量来实现。我建议安装Devel模块并添加dsm($form);到上面的功能中。


你怎么知道这可以给一个类来提交按钮$形式[“行动”] [“提交”] [“#属性”] [“类”] []
enjoylife


api显示为$ form ['#attributes'] = array('class'=> array('search-form')); 为什么要这样写。$ form ['actions'] ['submit'] ['#attributes'] ['class'] []。并有一个[]?谢谢
Enjoylife 2011年

7
如果愿意$form['actions']['submit']['#attributes']['class'] = array('form-submitone');,您将覆盖已设置的所有类。如果这样做$form['actions']['submit']['#attributes'] = array('class' => array('form-submitone'));,则不仅将覆盖现有的类,还将覆盖所有属性。(PS另请参见我对上述答案的补充。)
marcvangend 2011年

这也适用于Drupal 8。
Achraf JEDAY

0

此代码基本上由用户dor在表单提交按钮上添加您的自定义类。

function subscriber_form(){
    $form['submit'] = array(
    '#type'  => 'submit',
    '#attributes'=>array('class'=> array('mybtn')),
    '#ajax' => array(
      'callback' => 'subscriber_ajax_callback',
    ),
    '#value' =>'Submit', // @FIXME doesn't render quotes properly
  );

}

2
这是不正确的,'class' => array("mybtn-class", "mybtn-class2")
#attributes

尽管@devendra回答有效,但遵循@DrCord的建议,应该是'#attributes' => array('class' => array('mybtn', 'mybtn-2'))
johnatasjmo
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.