gpt4 book ai didi

javascript - 选择同一列中的所有复选框

转载 作者:行者123 更新时间:2023-11-28 15:04:47 24 4
gpt4 key购买 nike

我正在尝试实现类似于Checking checkbox in column的东西

我在表格中有两个全选复选框,选择一个将选择同一列的所有复选框。

它是一个 ASP.NET GridView。 Plunker

function CheckHeaderCheckboxAll(obj, gridname) {    
var objId = obj.id;
var table= $(obj).closest('table');
$('td input:checkbox',table).prop('checked',this.checked);
}

有人可以帮我解决这个问题吗?

最佳答案

基本思想是选择单元格,获取索引,然后选择具有该索引的表格的其他表格单元格。

$("th input[type='checkbox']").on("change", function() {
var cb = $(this), //checkbox that was changed
th = cb.parent(), //get parent th
col = th.index() + 1; //get column index. note nth-child starts at 1, not zero
$("tbody td:nth-child(" + col + ") input").prop("checked", this.checked); //select the inputs and [un]check it
});
th { background-color: #CCC; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>
<input type="checkbox">
</th>
<th>
<input type="checkbox">
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox">
</td>
<td>
<input type="checkbox">
</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>
<input type="checkbox">
</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>
<input type="checkbox">
</td>
</tr>
</tbody>
</table>

如果您想使用内联事件处理程序来完成此操作,那么您的代码将如下所示

function CheckHeaderCheckboxAll(obj) {
var cb = $(obj), //checkbox that was changed
th = cb.parent(), //get parent th
col = th.index() + 1; //get column index. note nth-child starts at 1, not zero
$("tbody td:nth-child(" + col + ") input").prop("checked", obj.checked); //select the inputs and [un]check it
}

关于javascript - 选择同一列中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490290/

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