比如这么一些列表,随便点击一个就能获取已经选中的个数。<input type="checkbox" name="checkbox"><input type="checkbox" name="checkbox"><input type="checkbox" name="checkbox"><input type="checkbox" name="checkbox"><input type="checkbox" name="checkbox"><input type="checkbox" name="checkbox">
2 回答

婷婷同学_
TA贡献1844条经验 获得超8个赞
function test() {
let checks = document.querySelectorAll('input[type=checkbox]');
let n = 0;
for(let i =0;i<checks.length;i++){
if(checks[i].checked){
n++
}
}
return n
}
let checks = document.querySelectorAll('input[type=checkbox]');
for (let j =0;j<checks.length;j++){
checks[j].onclick=function () {
console.log(test());
}
}

子衿沉夜
TA贡献1828条经验 获得超3个赞
添加回答
举报
0/150
提交
取消