Javascript 选中列中的所有复选框,并在选中其他列时取消选中它们
我一直很难做到这一点。我有两列带有复选框,每列中有一个复选框用于选择所有其他内容 问题是,当我选择另一列时,子复选框保持选中状态 我正在使用下一个jquery函数使其工作,但它只是部分工作 选择所有复选框的脚本:Javascript 选中列中的所有复选框,并在选中其他列时取消选中它们,javascript,jquery,html,Javascript,Jquery,Html,我一直很难做到这一点。我有两列带有复选框,每列中有一个复选框用于选择所有其他内容 问题是,当我选择另一列时,子复选框保持选中状态 我正在使用下一个jquery函数使其工作,但它只是部分工作 选择所有复选框的脚本: $("th input[type='checkbox']").on("change", function () { debugger; var cb = $(this), //checkbox that was changed th
$("th input[type='checkbox']").on("change", function () {
debugger;
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
});
仅选择所需列中的复选框的脚本:
$('table').attr('id', 'test');
$('input[type="checkbox"]').on('change', function () {
var checado = $(this).prop('checked');
$(this).closest('tr').find('input[type="checkbox"]').each(function () {
$(this).prop('checked', false);
});
$(this).prop("checked", checado);
});
Html:
对开本
德斯特诺银行
提波文件
埃斯塔多
符合
不符合
科门塔里奥斯
洛洛
2017100000793
058-班里乔
认证副本
恩特雷加多
2017100000790
058-班里乔
图像
阿滕迪多
感谢您的帮助您可以先取消选中所有复选框(当前单击的复选框除外),然后选中所需的复选框:
//Uncheck all the checkboxes except the current clicked
$("table input:checkbox").not(this).prop("checked", false);
希望这有帮助
$(“th input[type='checkbox'])。在(“change”,function()上{
调试器;
//取消选中除当前已单击的复选框之外的所有复选框
$(“表输入:复选框”).not(this).prop(“checked”,false);
var cb=$(此),//已更改的复选框
th=cb.parent(),//获取父项th
col=th.index()+1;//获取列索引。注意,第n个子项从1开始,而不是从零开始
$(“tbody td:n个子(“+col+”)input”).prop(“选中”,this.checked);//选择输入并[un]选中它
});代码>
对开本
德斯特诺银行
提波文件
埃斯塔多
符合
不符合
科门塔里奥斯
洛洛
2017100000793
058-班里乔
认证副本
恩特雷加多
2017100000790
058-班里乔
图像
阿滕迪多
//Uncheck all the checkboxes except the current clicked
$("table input:checkbox").not(this).prop("checked", false);