1 回答
TA贡献1874条经验 获得超12个赞
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="div1">
<input type="checkbox" value="1"> A
<input type="checkbox" value="2"> B
<input type="checkbox" value="3"> C
</div>
<div id="div2">
<input type="checkbox" value="a" data-parent="1"> a
<input type="checkbox" value="b" data-parent="1"> b
<input type="checkbox" value="c" data-parent="2"> c
<input type="checkbox" value="d" data-parent="2"> d
<input type="checkbox" value="e" data-parent="3"> e
</div>
<script>
var checks1 = document.getElementById("div1").getElementsByTagName("input");
var checks2 = document.getElementById("div2").getElementsByTagName("input");
for (var i=0;i<checks1.length;i++){
checks1[i].onclick = function () {
var cValue = this.value;
var isCheck = this.checked;
for (var j=0;j<checks2.length;j++){
if (checks2[j].dataset.parent==cValue){
checks2[j].checked =isCheck;
}
}
}
}
</script>
</body>
</html>
添加回答
举报