如何使用jQuery检查所有复选框? [英] How to check all checkboxes using jQuery?
本文介绍了如何使用jQuery检查所有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不是jQuery专家,但是我尝试为我的应用程序创建一个小脚本。我要选中所有复选框,但不能正常工作。
I am not expert with jQuery but I have tried to create a little script for my application. I want to check all checkboxes but it isn't working correctly.
首先,我尝试使用 attr
,之后我尝试使用 prop
但我做错了。
First I tried to use attr
and after that I tried with prop
but I'm doing something wrong.
我首先尝试了此方法:
$("#checkAll").change(function(){
if (! $('input:checkbox').is('checked')) {
$('input:checkbox').attr('checked','checked');
} else {
$('input:checkbox').removeAttr('checked');
}
});
但这没有用。
下一步:比上面的代码效果更好
Next: This worked better than above code
$("#checkAll").change(function(){
if (! $('input:checkbox').is('checked')) {
$('input:checkbox').prop('checked',true);
} else {
$('input:checkbox').prop('checked', false);
}
});
两个示例都不起作用。
JSFiddle: http://jsfiddle.net/hhZfu/4/
JSFiddle: http://jsfiddle.net/hhZfu/4/
推荐答案
您需要使用 .prop()进行设置选中的属性
You need to use .prop() to set the checked property
$("#checkAll").click(function(){
$('input:checkbox').not(this).prop('checked', this.checked);
});
演示:小提琴
这篇关于如何使用jQuery检查所有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文