我从stackoverflow获得了这个代码,它看起来像一个非常好的"全选"复选框解决方案,任何想法为什么它在第二次点击后失败?
<table>
<tr>
<td>
<input type='checkbox' value='0' class=''>
</td>
<td>
<input type='checkbox' value='0' class=''>
</td>
<td>
<input type='checkbox' value='0' class=''>
</td>
<td width="100" align="right">
select all <input type='checkbox' value='0' class='selectall2'>
</td>
</tr>
</table>
$(document).ready(function () {
$(document).on("click", ".selectall2", function () {
$(this).closest('tr').find('input[type=checkbox]').attr('checked', this.checked);
});
});
Run Code Online (Sandbox Code Playgroud)
pkt*_*yue 19
使用.prop()而不是.attr()上面的jQuery 1.6
如果使用jQuery 1.6,代码if ( $(elem).attr("checked") )将检索实际内容属性,该属性在选中和取消选中复选框时不会更改.它仅用于存储checked属性的默认值或初始值.为了保持向后兼容性,.attr()jQuery 1.6.1+中的方法将为您检索和更新属性,因此不需要将布尔属性的代码更改为.prop().然而,检索已检查值的首选方法是使用上面列出的选项之一.要在最新的jQuery中查看其工作原理,请选中/取消选中下面示例中的复选框.
看到 .prop()
演示 - http://jsfiddle.net/f9QYx/