用Js写全选反选的功能
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//全选
function checkAll(name)
{
var names=document.getElementsByName(name);
var len=names.length;
if(len>0)
{
var i=0;
for(i=0;i<len;i++)
names[i].checked=true;
}
}
//全不选
function checkAllNo(name)
{
var names=document.getElementsByName(name);
var len=names.length;
if(len>0)
{
var i=0;
for(i=0;i<len;i++)
names[i].checked=false;
}
}
//反选
function reserveCheck(name)
{
var names=document.getElementsByName(name);
var len=names.length;
if(len>0)
{
var i=0;
for(i=0;i<len;i++)
{
if(names[i].checked)
names[i].checked=false;
else
names[i].checked=true;
}
}
}
</script>
</head>
<body>
周末的时候做什么呢?
<br />
<input type="checkbox" name="ck" value="1" />跑步
<br />
<input type="checkbox" name="ck" value="2" />看电影
<br />
<input type="checkbox" name="ck" value="3" />读书
<br />
<input type="checkbox" name="ck" value="4" />美食
<br />
<input type="checkbox" name="ck" value="5" />睡觉
<br />
<a href="javaScript:checkAll('ck')">全选</a>
<a href="javaScript:reserveCheck('ck')">反选</a>
<a href="javaScript:checkAllNo('ck')">取消</a>
</body>
</html>