js控制复选框checkbox参考代码

时间:2022-03-05 09:25:05
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>checkbox</title>
<script type="text/javascript">

function cheall(){
var box=document.getElementById("cha");
if(box.checked==true){
document.getElementById("ch1").checked=true;
document.getElementById("ch2").checked=true;
document.getElementById("ch3").checked=true;
} else {
document.getElementById("ch1").checked=false;
document.getElementById("ch2").checked=false;
document.getElementById("ch3").checked=false;
}
}
function ch(){
var box1=document.getElementById("ch1");
var box2=document.getElementById("ch2");
var box3=document.getElementById("ch3");
if(box1.checked==true && box2.checked==true && box3.checked==true){
document.getElementById("cha").checked=true;
} else {
document.getElementById("cha").checked=false;
}
}
</script>
</head>

<body>
<table width="600" border="1" style="border-collapse:collapse; font-size:12px">
<tr>
<td>科室:</td>
<td>
<input type="checkbox" name="ch1" id="ch1" title="科室1" onclick="ch();"/>
<label for="ch1">科室1</label> 
<input type="checkbox" name="ch2" id="ch2" title="科室2" onclick="ch();"/>
<label for="ch2">科室2</label> 
<input type="checkbox" name="ch3" id="ch3" title="科室3" onclick="ch();"/>
<label for="ch3">科室3</label>
</td>
</tr>
<tr>
<td>车间:</td>
<td> </td>
</tr>
<tr>
<td>多选</td>
<td> <input type="checkbox" name="cha" id="cha" title="所有科室" onclick="cheall();" /><label for="ch1">所有科室</label> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>

</body>
</html>