<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;
}
}
</script>
html:
<input type="checkbox" name="checkbox" value="checkbox" onclick="if(this.checked==true) { checkall('gonggaoid'); } else { checkallno('gonggaoid'); }" />
全選</label></td>
<input type="checkbox" name="gonggaoid" value="abc" />
方法二
<html>
<head>
<script type="text/javascript教程">
function checkevent(name,allcheckid)
{
var allck=document.getelementbyid(allcheckid);
if(allck.checked==true)
checkall(name);
else
checkallno(name);
全選
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>
<input type="checkbox" id="ckall" onclick="checkevent('ck','ckall')" />全選
<input type="checkbox" id="ckreserve" onclick="reservecheck('ck','ckreserve')" />反選
<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" />睡覺
</body></html>