在我正在编码的模块中,我有以下代码:
'SelectType' => array(
'#type' => 'select',
'#name' => 'dropdown',
'#options' => drupal_map_assoc(array(
'Keyword-classic','Keyword-Encore','Reserves: Instructor',)),
'#attributes' => array('id' => array('SelectType'),
'onchange' => "change_action('catalogsearch', this.selectedIndex)",
),
),
这会产生这样的结果:
[...]
<select id="SelectType"
onchange="change_action('catalogsearch', this.selectedIndex)"
name="dropdown" class="form-select">
[...]
我需要它来生成(在第三行输出 ' 而不是 '):
[...]
<select id="SelectType" style="float:left;"
onchange="change_action('catalogsearch', this.selectedIndex)"
name="dropdown" class="form-select">
[...]
我需要更改什么才能使其正常工作?
最佳答案
你可以尝试这样:
'SelectType' => array(
'#type' => 'select',
'#name' => 'dropdown',
'#options' => drupal_map_assoc(array(
'Keyword-classic','Keyword-Encore','Reserves: Instructor',)),
'#attributes' => array('id' => array('SelectType'),
'onchange' => 'change_action(\'catalogsearch\', this.selectedIndex)',
),
),
顺便说一下,由于 ID
是唯一的,并且一个元素一次只能使用一个,因此您应该使用 'id' => 'SelectType'
而不是 'id' => array('SelectType')
。
编辑:
如果上面的代码不起作用,那么您可以使用jQuery
,如下所示:
$("#SelectType").change(function() {
YOUR CODE.....
});
关于javascript - 如何正确地将 onchange 属性添加到 Drupal 7 中的表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17432186/