我在Drupal 7中有一个选择表单控件。如何在onchange事件触发时调用函数。
这是我的选择表单控件代码:
$form['id']['signature'] = array(
'#type' => 'select',
'#options' => array(
0 => t('Browse...'),
1 => t('Sign...'),
2 => t('Clear...'),
),
);
答案 0 :(得分:0)
您可以使用' #attibutes' 例如。
$form['id']['signature']['#attributes'] = array('onchange' => array('myFunction()'))
虽然我从不喜欢向html标签添加事件属性,所以你也可以通过在行为中添加内联(或外部)javascript来实现这一点:
$form['id']['signature'] = array(
'#type' => 'select',
'#options' => array(
0 => t('Browse...'),
1 => t('Sign...'),
2 => t('Clear...'),
),
'#attributes' => array(
'id' => array('signature-goes-here'),
),
);
$form['id']['signature']['#attached']['js'][] = array(
'data' => "
Drupal.behaviors.signature = function (context) {
$('#signature-goes-here', context).change(function () {
// Do stuff here.
});
}
",
'type' => 'inline',
);
但是如果你想在这个表单项改变时激活ajax,你可能会使用Drupal表单api功能。