[英]Check all checkboxes on page load with jQuery
在页面加载时,使用jQuery如何自动选择特定div中的所有复选框?
$(function(){
$('#thediv input:checkbox').attr('checked', 'checked');
});
People seem genuinely confused about how to do this in jQuery. 人们似乎对如何在jQuery中执行此操作感到困惑。 Checking a checkbox is much easier and more efficient without it.
没有它,检查复选框会更容易,更有效。 The following uses jQuery's strengths of selecting and iterating over a list of nodes combined with the couldn't-be-simpler DOM
checked
property of checkbox elements: 以下使用jQuery的优势来选择和迭代节点列表,结合复选框元素的无法更简单的DOM
checked
属性:
$("#myDiv input:checkbox").each(function() {
this.checked = true;
});
It's not too hard to cut out jQuery altogether in this case: 在这种情况下完全删除jQuery并不太难:
var div = document.getElementById("myDiv");
var inputs = div.getElementsByTagName("input");
for (var i = 0, len = inputs.length; i < len; ++i) {
if (inputs[i].type == "checkbox") {
inputs[i].checked = true;
}
}
UPDATE UPDATE
Since the release of jQuery 1.6 and prop()
, there is a sane way to do this in jQuery: 自从jQuery 1.6和
prop()
发布以来,在jQuery中有一种理智的方法:
$("#myDiv input:checkbox").prop("checked", true);
What redquare has will work, but it's a better idea overall to use true
/ false
with these since that's how they're normalized, here are a few examples of why this is a handy habit to follow. redquare有什么用 ,但是总体上使用
true
/ false
是个更好的主意,因为这是他们的规范化,这里有几个例子说明为什么这是一个方便的习惯。 For example: 例如:
$('#myDiv :checkbox').attr('checked', 'checked');
alert($('#myDiv :checkbox').attr('checked')); //this alerts true, not "checked"
You can test this here 你可以在这里测试一下
Instead, get in the habit of passing a boolean, which is a lot more versatile, for example: 相反,养成传递布尔值的习惯,布尔值更通用,例如:
$(function() {
$('#myDiv :checkbox').attr('checked', true);
});
This allows a lot more flexibility in more terse code, for example, say we had a "Check All" checkbox on the top, how would that look? 这样可以在更简洁的代码中提供更大的灵活性,例如,我们在顶部有一个“全部检查”复选框,看起来如何?
$('#checkAll').change(function() {
if(this.checked) {
$('#subTable :checkbox').attr('checked', 'checked');
} else {
$('#subTable :checkbox').removeAttr('checked', 'checked');
}
});
Now we've had to bring a new function in to do the work, .removeAttr()
. 现在我们必须带一个新函数来完成工作,
.removeAttr()
。 To be fair, this is even how the jQuery documentation does it in the example . 公平地说, 这甚至是jQuery文档在示例中的用法 。 But, you can cut that down greatly if you take advantage of the normalization jQuery does internally, like this:
但是,如果你利用jQuery在内部执行的规范化,你可以大大减少它,如下所示:
$('#checkAll').change(function() {
$('#subTable :checkbox').attr('checked', this.checked);
});
This is just something to keep in mind as you code, the same rules appy to .attr('disabled')
. 在编写代码时,请注意这一点,相同的规则应用于
.attr('disabled')
。 There are other areas that normalization occurs as well...probably events being the most extensive of them, if you're curious you can see that here , it's already done for you, use it :) 还有其他区域也会发生规范化......可能事件是其中最广泛的,如果你很好奇, 你可以在这里看到 ,它已经为你完成了,使用它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.