It certainly is in
jQuery 1.6+
Use the new .prop() function:
$('.myCheckbox').prop('checked',true);
$('.myCheckbox').prop('checked',false);
jQuery 1.5 and below
The .prop() function is not available, so you need to use .attr().
To check the checkbox (by setting the value of the checked attribute) do
$('.myCheckbox').attr('checked','checked');
and for un-checking (by removing the attribute entirely) do
$('.myCheckbox').removeAttr('checked');
Any version of jQuery
If you're working with just one element, it will always be fastest to use DOMElement.checked = true. The benefit to using the .prop() and .attr() functions is that they will operate on all matched elements.
// Assuming an event handler on a checkboxif(this.checked)
本文介绍了如何使用jQuery的不同版本来设置和取消设置复选框。对于1.6及更高版本,推荐使用.prop()方法;而对于1.5及更早版本,则应使用.attr()方法。此外,直接操作DOM元素也是一种有效的方法。
436

被折叠的 条评论
为什么被折叠?



