// 选中所有的复选框
function chkAllEvent(obj){
chkCount = 0;
for(i = 0; i < chk.length; i++) {
chk[i].checked = true;
chkCount++;
}
// 更改复选框后面的文字
obj.nextSibling.nodeValue = "取消";
// 更改复选框的点击事件
obj.onclick = function(){celAllEvent(obj)}
}
// 选中的复选框取消
function celAllEvent(obj){
chkCount = 0;
for(i = 0; i < chk.length; i++) {
chk[i].checked = false;
}
// 更改复选框后面的文字
obj.nextSibling.nodeValue = "全选";
// 更改复选框的点击事件
obj.onclick = function(){chkAllEvent(obj)}
}
// 记录已选中的数量
var chkCount=0;
// 单个复选框调用
function chkOneEvent(obj){
if(obj.checked){
chkCount++;
if(chkCount == chk.length){
// 所有的都被选中的场合
chkAll.checked = true;
chkAll.nextSibling.nodeValue = "取消";
chkAll.onclick = function(){celAllEvent(chkAll)}
}
} else {
chkCount--;
if(chkCount == chk.length - 1){
// 从全选状态下去掉一个的场合
chkAll.checked = false;
chkAll.nextSibling.nodeValue = "全选";
chkAll.onclick = function(){chkAllEvent(chkAll)}
}
}
}
[JavaScript]事件切换之-多选框全选
最新推荐文章于 2024-08-04 16:33:59 发布