【发布时间】:2012-11-01 18:29:28
【问题描述】:
我需要这个功能在页面加载后才能工作。 错误是缺失的;在真实行上的语句错误之前。 此外,当复选框切换全部被单击为“已选中”时,我希望它将表中的类 checkall 标记为 true,并且当再次单击切换所有复选框时,将表中的所有复选框标记为 false。 看起来几乎是正确的,但括号有些错误。
$(document).ready(function() {
$('#toggle-all, input:checkbox').click(
function () {
$('#table, :checkbox').attr(':checked','checked').true);
},function(){
$('#table :checkbox').attr(':checked').false);
}
);
});
<section id="main">
<form id="task-list"> <input id="toggle-all" name="toggle-all" type="checkbox" />
</form>
</section>
<table class="table" >
<tr>
<td><input type="checkbox" class="checkall" />item.todo </td>
<td> item.name</td><td>
</tr>
...... more rows
</table>
【问题讨论】:
-
将您的代码放到 jsfiddle.net 上,我们很容易为您提供帮助。
-
仅供参考:您不能将两个函数分配给单击,第一个函数引用#table 和复选框,第二个函数是#table 中的复选框(加上#表不存在,.table 存在)。您应该使用 prop 而不是 attr,并且不要使用“.false”或“.true”。无论如何,下面的工作解决方案。
标签: javascript jquery checkbox toggle