JavaScript写的一个列表实现CheckBox列的多选和取消多选代码
<html>
<head>
<script type="text/javascript" language="javascript">
function selectAll(control)
{
var arrAll=document.getElementsByTagName("input");
for(var i=0;i<arrAll.length;i++)
{
if(arrAll[i].type=='checkbox')
{
document.getElementById(arrAll[i].id).checked=document.getElementById(control.id).checked;
}
}
}
function selSome()
{
var selAll=document.getElementById("chkAll");
var arrAll=document.getElementsByTagName("input");
var count=0;
var countCB=0;
for(var i=0;i<arrAll.length;i++)
{
if(arrAll[i].type=="checkbox"&&arrAll[i].id!="chkAll")
{
countCB++;
if(arrAll[i].checked)
{
count++;
}
}
}
selAll.checked=(count===countCB);
}
</script>
</head>
<body>
<table>
<th>
<td><input type="checkbox" id="chkAll" onclick="selectAll(this)"></td>
</th>
<tr>
<td>
<input type="checkbox" id="cb1" onclick="selSome()">
<td>
</tr>
<tr>
<td>
<input type="checkbox" id="cb2" onclick="selSome()" >
<td>
</tr>
<tr>
<td>
<input type="checkbox" id="cb3" onclick="selSome()">
<td>
</tr>
<tr>
<td>
<input type="checkbox" id="cb4" onclick="selSome()">
<td>
</tr>
<tr>
<td>
<input type="checkbox" id="cb5" onclick="selSome()">
<td>
</tr>
<tr>
<td>
<input type="checkbox" id="cb6" onclick="selSome()">
<td>
</tr>
<tr>
<td>
<input type="checkbox" id="cb7" onclick="selSome()">
<td>
</tr>
</table>
</body>
</html>