//注册全选事件 $("#selectAll").click(function () { var isAllCheck = $("#selectAll").prop("checked"); var items = document.getElementsByName("ckbx"); if (isAllCheck) { //全选 for (var i = 0; i < items.length; i++) { $(items[i]).prop('checked', true); } } else { //取消全选 for (var i = 0; i < items.length; i++) { $(items[i]).prop('checked', false); } } }); //注册单个选中事件,全选了“全选”按钮也勾上,否则“全选”按钮去掉勾选 var ckboxs = document.getElementsByName("ckbx"); for (let i = 0; i < ckboxs.length; i++) { ckboxs[i].onclick = function () { //默认都勾取 let flag = true; for (j = 0; j < ckboxs.length; j++) { if (ckboxs[j].checked == false) { //如果checked如果有一个没选上就是false flag = false; } } selectAll.checked = flag; } }
标签:checked,ckboxs,items,js,全选,单选,var,false From: https://www.cnblogs.com/privateLogs/p/17380622.html