[英]Drupal 7 Form api Custom javascript with Jquery
我正在使用Drupal 7开发带有表单API的自定义表单以及带有复选框的tableselect。 我对选择要使用jquery进行编码的复选框有一些限制。 我为onclick复选框创建了一个功能,在drupal之外,它的工作方式就像您可以在这里看到的一样,但是当我尝试与drupal集成时,jquery选择器根本不起作用。
所以这是我在drupal中的代码:
$form ['#attached'] ['js'] = array (drupal_get_path ( 'module',
'form_cuentacorriente' ) . '/checkboxes.js' );
$form ['tabla'] = array (
'#type' => 'tableselect',
'#header' => $tabla ['header'],
'#options' => $tabla ['body'],
'#attributes' => array ('id' => 'conceptos')
);
这是我的js函数:
(function($){
$('input[type=checkbox]').click(function(){
var tabla = $("#conceptos")[0];
var long = tabla.rows.length;
var pos = $(this).closest("tr").index();
if (!this.checked){
for (i = pos+1; i < long +1; i++) {
$(tabla.rows[i]).find( 'input' ).prop('disabled', !this.checked).prop('checked', false);
}
}else{
$(this).closest("tr").next().find("input")
.prop('disabled', !this.checked);
}
});
})(jQuery);
如果我使用*选择器,则在单击它的任何地方都可以使用,但是'input [type = checkbox]'不起作用。 Drupal 7上的JQuery 1.10版本
任何想法,我想念的是什么?
提前致谢!
解决:由于Drupal的行为,我只需要像这样包装我的js函数:
Drupal.behaviors.form_cuentacorriente = {
attach: function (context, settings) {
$('input[type=checkbox]').click(function(){
var tabla = $("#conceptos")[0];
var long = tabla.rows.length;
var pos = $(this).closest("tr").index();
if (!this.checked){
for (i = pos+1; i < long +1; i++) {
$(tabla.rows[i]).find( 'input' ).prop('disabled',
!this.checked).prop('checked', false);
}
}else{
$(this).closest("tr").next().find("input")
.prop('disabled', !this.checked);
}
});
}
参考: https : //www.lullabot.com/articles/understanding-javascript-behaviors-in-drupal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.