hook_form中使用Drupal.attachBehaviors

如果form中有ajax事件,可能需要重新绑定事件,可以这样解决,代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Implements hook_form_alter().
*/
function xxx_form_alter(&$commands, &$form, $form_state) {
//用于Drupal.attachBehaviors(document)
$commands[] = ajax_command_invoke(NULL, 'xxx_form_alter_attach_behaviors');
}
(function($) {
Drupal.behaviors.xxx_form = {
attach: function (context, settings) {
//js here
}
}
$.fn.xxx_form_alter_attach_behaviors = function() {
Drupal.attachBehaviors(document);
}
})(jQuery);
script>