我试图在jquery中创建一个函数,以便在单击一个主复选框时禁用一组复选框
功能
function DisableCheckboxes(id)
{
if($(this).is(':checked'))
{
$("input[name="+id+"]").each(function(){
$(this).attr({
'checked':false,
'disabled':true});
});
}
else
{
$("input[name="+id+"]").each(function(){
$(this).attr('disabled',false);
});
}
}
并在点击事件中将其称为 -
$(document).ready(function() {
$("#chkNotMatter").click(DisableCheckboxes('chklStatus'));
});
问题 点击事件donot得到应用,主复选框被检查
答案 0 :(得分:5)
您应该将回调传递给事件处理程序,而是调用DisableCheckboxes
并返回结果。你应该使用:
$(document).ready(function() {
$("#chkNotMatter").click(function() { DisableCheckboxes.call(this, 'chklStatus'); });
});
修改强>
如更新的代码段所示,您必须在call
上使用DisableCheckboxes
,因为您在其上引用this
,因此它正确引用了复选框dom元素。
此外,您可以按如下方式缩小DisableCheckboxes
,因为attr
会修改选择器中的所有对象:
function DisableCheckboxes(id) {
if ($(this).is(':checked')) {
$("input[name="+id+"]").attr({
'checked':false,
'disabled':true
});
} else {
$("input[name="+id+"]").attr('disabled',false);
}
}
答案 1 :(得分:1)
$(document).ready(function(){
$('#mainCheckboxID').bind('change', function () {
if ($(this).is(':checked'))
$("input.group1").attr("disabled", true);
else
$("input.group1").removeAttr("disabled");
});
});