<input type="checkbox" name="test" onclick="checkedThis(this);">1
<input type="checkbox" name="test" name="2" onclick="checkedThis(this);">2
<input type="checkbox" name="test" name="3" onclick="checkedThis(this);">3
<input type="checkbox" name="test" name="4" onclick="checkedThis(this);">4
<input type="checkbox" name="test" name="5" onclick="checkedThis(this);">5
function checkedThis(obj){
var boxArray = document.getElementsByName('test');
for(var i=0;i<=boxArray.length-1;i++){
if(boxArray[i]==obj && obj.checked){
boxArray[i].checked = true;
}else{
boxArray[i].checked = false;
}
}
}
<input type="checkbox" name="test" name="2" onclick="checkedThis(this);">2
<input type="checkbox" name="test" name="3" onclick="checkedThis(this);">3
<input type="checkbox" name="test" name="4" onclick="checkedThis(this);">4
<input type="checkbox" name="test" name="5" onclick="checkedThis(this);">5
function checkedThis(obj){
var boxArray = document.getElementsByName('test');
for(var i=0;i<=boxArray.length-1;i++){
if(boxArray[i]==obj && obj.checked){
boxArray[i].checked = true;
}else{
boxArray[i].checked = false;
}
}
}
本文介绍了一个简单的JavaScript函数,用于实现一组同名复选框的批量选择与取消选择功能。通过点击任一复选框,该函数可以使得所有同名复选框的状态保持一致。
3万+

被折叠的 条评论
为什么被折叠?



