Jquery 选中相应列中的所有复选框
如果没有类或id,如何选中同一列中的所有复选框?我的表格截图、HTML和jquery代码如下: 屏幕截图 请注意,我使用了rowspan和colspan,它会影响任何东西吗? HTML代码Jquery 选中相应列中的所有复选框,jquery,html,Jquery,Html,如果没有类或id,如何选中同一列中的所有复选框?我的表格截图、HTML和jquery代码如下: 屏幕截图 请注意,我使用了rowspan和colspan,它会影响任何东西吗? HTML代码 $("th p input[type='checkbox']").on("change", function() { var cb = $(this), //checkbox that was changed th = cb.parent(), //get
$("th p input[type='checkbox']").on("change", function() {
var cb = $(this), //checkbox that was changed
th = cb.parent(), //get parent th
col = th.index() + 2; //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
});
当我点击Read复选框时,相同的列将被选中。但当我单击Create列时,它仍然会触发Read列的复选框
<table class="table table-bordered" id="table_perm">
<thead>
<tr>
<th rowspan="2">Modules (Page)</th>
<th colspan="4">Permissions</th>
</tr>
<tr>
<th>Read <p><input type="checkbox" class="big-checkbox"></p></th>
<th>Create <p><input type="checkbox" class="big-checkbox"></p></th>
<th>Edit <p><input type="checkbox" class="big-checkbox"></p></th>
<th>Delete <p><input type="checkbox" class="big-checkbox"></p></th>
</tr>
</thead>
<tbody>
<tr data-id="1">
<td>Page 1</td>
<td><input type="checkbox" class="big-checkbox" value="r"></td>
<td><input type="checkbox" class="big-checkbox" value="c"></td>
<td><input type="checkbox" class="big-checkbox" value="e"></td>
<td><input type="checkbox" class="big-checkbox" value="d"></td>
</tr><tr data-id="6">
<td>Page 2</td>
<td><input type="checkbox" class="big-checkbox" value="r"></td>
<td><input type="checkbox" class="big-checkbox" value="c"></td>
<td><input type="checkbox" class="big-checkbox" value="e"></td>
<td><input type="checkbox" class="big-checkbox" value="d"></td>
</tr>
</tbody>
</table>
.parent()
指向p
,而不是th
所以
使用最近的(“th”)
获取最近的th
$(“th input[type='checkbox'])。在(“change”,function()上{
var$cb=$(此),
$th=$cb.closest(“th”),//获取父项th
col=$th.index()+2;//获取列索引。注意,第n个子项从1开始,而不是从零开始
$(“tbody td:n个子(“+col+”)input”).prop(“选中”,this.checked);//选择输入并[un]选中它
});代码>
模块(第页)
权限
阅读
创建
编辑
删除
第1页
第2页
复选框的父项是一个段落标记,而不是th
您是否也可以帮助处理缺少的部分?我猜逻辑是签入tbody
如果任何复选框被取消选中,那么它将确保相应的th
复选框也被取消选中。“我说得对吗?”是的。逻辑非常相似,复制粘贴您拥有的内容并使用正确的选择器