gpt4 book ai didi

jquery - 如何通过选中/取消选中一个复选框来选中和取消选中 html 表中的所有复选框?

转载 作者:行者123 更新时间:2023-12-03 22:44:04 25 4
gpt4 key购买 nike

我有一个表格,有行,每行都有一个复选框,头部有一个主复选框。我的代码:

<table border="1">
<thead>
<tr>
<th><input type="checkbox" id="allcb" name="allcb"/></th>
<th>values</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" id="cb1" name="cb1"/></td>
<td>value1</td>
</tr>
<tr>
<td><input type="checkbox" id="cb2" name="cb2"/></td>
<td>value2</td>
</tr>
<tr>
<td><input type="checkbox" id="cb3" name="cb3"/></td>
<td>value3</td>
</tr>
</tbody>
</table>

(也试试 here )

任何人都可以帮助我,如何做到这一点,如果我选中顶部的主复选框,所有复选框都将被选中,如果我取消选中主复选框,所有复选框都将被取消选中。如果您帮助我,谢谢!

最佳答案

工作演示 http://jsfiddle.net/xYAfj/2/

$('#allcb').change(function(){
if($(this).prop('checked')){
$('tbody tr td input[type="checkbox"]').each(function(){
$(this).prop('checked', true);
});
}else{
$('tbody tr td input[type="checkbox"]').each(function(){
$(this).prop('checked', false);
});
}
});

较短的代码

工作演示 http://jsfiddle.net/cse_tushar/4tss8/

$('#allcb').change(function () {
$('tbody tr td input[type="checkbox"]').prop('checked', $(this).prop('checked'));
});

关于jquery - 如何通过选中/取消选中一个复选框来选中和取消选中 html 表中的所有复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17814005/

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