复选框全选的一个问题,只有一个时选不中,多个就可以?急用!
如何有一个时也可以选中。
我用了下面一段代码:
<script language= "javascript ">
function selectAll()
{
for (var i=0;i <document.form2.del.length;i++) {
var temp=document.form2.del[i];
temp.checked=!temp.checked;
}
if (document.form2.selectButton.value== "全选 ")
{
document.form2.selectButton.value= "取消 ";
}
else
{
document.form2.selectButton.value= "全选 ";
}
}
</script>
------解决方案--------------------temp.checked=!temp.checked;
点击全选,应该全部选择对不对?但你事先中其中一个试试,明显没有达到全选的效果!
下面给你我的代码!
<html>
<head>
<title> test </title>
<script language= "javascript ">
function checkAll(name)
{
var el = document.getElementsByTagName( 'input ');
var len = el.length;
for(var i=0; i <len; i++)
{
if((el[i].type== "checkbox ") && (el[i].name==name))
{
el[i].checked = true;
}
}
}
function clearAll(name)
{
var el = document.getElementsByTagName( 'input ');
var len = el.length;
for(var i=0; i <len; i++)
{
if((el[i].type== "checkbox ") && (el[i].name==name))
{
el[i].checked = false;
}
}
}
function selClick()
{
if (document.form1.selectButton.value== "全选 ")
{
checkAll( "del ");
document.form1.selectButton.value= "取消 ";
}
else
{
clearAll( "del ");
document.form1.selectButton.value= "全选 ";
}
}
</script>
</head>
<body>
<form id= "form1 " name= "form1 " method= "post " action= " ">
<input type= "checkbox " name= "del " value= "checkbox " />
<input type= "checkbox " name= "del " value= "checkbox " />
<input type= "checkbox " name= "del " value= "checkbox " />
<input type= "checkbox " name= "del " value= "checkbox " />
<input type= "button " name= "selectButton " onClick= "selClick() " value= "全选 " />
</form>
</body>
</html>