gpt4 book ai didi

javascript - 单击功能后切换表格中的复选框

转载 作者:行者123 更新时间:2023-12-02 19:13:54 26 4
gpt4 key购买 nike

我需要这个函数在页面加载后工作。错误是缺少 ;真实行上的 before 语句错误。另外,当复选框切换全部被单击为“已选中”时,我希望它将表中的类标记为已选中,并且当再次单击切换所有复选框时,将表中的所有选中框标记为假。看起来几乎是正确的,但括号有一些错误。

$(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>

最佳答案

简单的解决方案:

$(document).ready(function() {

$("#toggle-all").click(function() {
$(".table input:checkbox").each(function() {
$(this).prop("checked", !$(this).prop("checked"));
});
});

});

更加防弹(始终将所有复选框一起切换):

$(document).ready(function() {

$("#toggle-all").click(function() {
$(".table input:checkbox").prop("checked", $(this).prop("checked"));
});

});​

演示:http://jsfiddle.net/Ck43Z/1/

关于javascript - 单击功能后切换表格中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13358855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com