87,904
社区成员
发帖
与我相关
我的任务
分享
$(function(){
$("#table thead").on("click", ":checkbox", function(event){
var i = $(this).closest("th").index()+1;
var v = $(this).prop("checked");
$("#table tbody td:nth-child("+i+") :checkbox").prop("checked",v);
});
$("#table tbody").on("click", ":checkbox", function(event){
var i = $(this).closest("td").index()+1;
var v = $("#table tbody td:nth-child("+i+") :checkbox:not(:checked)").length==0;
var indeterminate = !v && $("#table tbody td:nth-child("+i+") :checkbox").length>$("#table tbody td:nth-child("+i+") :checkbox:not(:checked)").length;
$("#table thead th:nth-child("+i+") :checkbox").prop("checked",v).prop("indeterminate",indeterminate);
});
});
楼上对,优化了一个中间态
$(function(){
$("#table thead").on("click", ":checkbox", function(event){
var i = $(this).closest("th").index()+1;
var v = $(this).prop("checked");
$("#table tbody td:nth-child("+i+") :checkbox").prop("checked",v);
});
$("#table tbody").on("click", ":checkbox", function(event){
var i = $(this).closest("td").index()+1;
var v = $("#table tbody td:nth-child("+i+") :checkbox:not(:checked)").length==0;
$("#table thead th:nth-child("+i+") :checkbox").prop("checked",v);
});
});