复制代码 代码如下:
<script language=javascript>
//第一种方法
function selectall1()
{
var a = document.getElementsByTagName("input");
if(a[0].checked==true){
for (var i=0; i<a.length; i++)
if (a[i].type == "checkbox") a[i].checked = false;
}
else
{
for (var i=0; i<a.length; i++)
if (a[i].type == "checkbox") a[i].checked = true;
}
}
//第二种方法
function selectall2() {
var tform = document.forms['form1'];
for (var i=0;i<tform.length;i++)
{
var e = tform.elements[i];
if (e.type == "checkbox")
e.checked = !e.checked;
}
}
//第三种方法,结合上述两种方法
function selectall3()
{
var a = document.getElementsByTagName("input");
for (var i=0; i<a.length; i++)
if (a[i].type == "checkbox") a[i].checked =!a[i].checked;
}
//第四种方法
function selectall4(id){ //用id区分
var tform=document.forms['form1'];
for(var i=0;i<tform.length;i++){
var e=tform.elements[i];
if(e.type=="checkbox" && e.name==id) e.checked=!e.checked;
}
}
//第五种方法
function selectall(theform,thename){ //theform指定的form,thename是checkbox的name属性
var tform=document.forms[theform];
document.getElementById("thewen").value='反选';
for(var i=0;i<tform.length;i++){
var e=tform.elements[i];
if(e.type=='checkbox' && e.name==thename)e.checked=!e.checked;
}
}
</script>
<form id="form1" name="form1" method="post" action="">
<input type="checkbox" name="sid" value="1" />
<input name="thes" type="button" onclick="javascript:selectall3()" value="全选" />
</form>