jquery设置所有复选框 [英] jquery set all checkbox checked
本文介绍了jquery设置所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用jquery 1.11.1,这是我的代码:
I am using jquery 1.11.1 and this is my code:
$("#rowchkall").change(function(){
if($(this).is(':checked')){
$("input:checkbox[class=rowchk]").each(function() {
alert("set checked");
$(this).attr('checked', "checked");
});
}else{
$("input:checkbox[class=rowchk]").each(function() {
$(this).attr('checked', false);
});
}
});
当我第一次点击 #rowchkall
所有复选框都设置为选中。当我再次单击它,所有复选框未选中。
When I first click on #rowchkall
, all the checkbox is set to checked. When I click it again, all checkbox is unchecked.
当我再次单击它时,警报框仍会出现,但不会选中任何复选框。为什么它只工作第一次?如何解决?
When I click it again, alert box still appear but none of the checkbox will be checked. Why is it only work for first time only? How can I fix it?
谢谢。
推荐答案
.prop()
,而不是 .attr()
$(this).prop('checked', true); //true to check else false uncheck
您的代码可以简化为
$("#rowchkall").change(function () {
$("input:checkbox.rowchk").prop('checked',this.checked);
});
这篇关于jquery设置所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文