js表单中checkbox全选
是初学者
想要做一个js功能像邮箱中收件箱的复选框选全选,
在选种第一个checkbox(name= "allcheckbox ")是会把下面的全部也选中,但下面的有一个被取消时(不选中),第一个checkbox(name= "allcheckbox ")的打钩也会去掉
还有就是 checkbox中的name的值 要求要不一样
急~~谢谢帮忙~~~~
<script language= "JavaScript "> ????? </script>
<form name= 'f1 ' id= 'f1 '>
<input name= "allcheckbox " type= "checkbox " title= '全选/取消 ' onclick= "cball() "> 全选 <br/>
<input name= "m1 " type= "checkbox " onclick= ' '> <br/>
<input name= "m2 " type= "checkbox " onclick= ' '> <br/>
<input name= "m3 " type= "checkbox " onclick= ' '> <br/>
<input name= "m4 " type= "checkbox " onclick= ' '> <br/>
<input name= "m5 " type= "checkbox " onclick= ' '> <br/>
<input name= "m6 " type= "checkbox " onclick= ' '> <br/>
</form>
------解决方案-------------------- <body>
<script language= "javascript ">
function cball(){
//alert(document.getElementById( "all ").checked)
var oform=document.getElementById( "f1 ");
var oc=oform.childNodes;
for (i=0;i <oc.length;i++){
if ((oc(i).type)== "checkbox ")
oc(i).checked=(document.getElementById( "all ").checked?true:false);
}
}
function cnall(obj){
var ock=document.getElementById( "all ");
var noChek=0;
if (obj.checked==false)
ock.checked=false;
var oform=document.getElementById( "f1 ");
var oc=oform.childNodes;
//alert(obj.checked);
for (i=0;i <oc.length;i++){
if ((oc(i).type)== "checkbox "){
if (oc(i).checked==true)
noChek+=1;
}
}
//alert(noChek);
if (noChek==6)
ock.checked=true;
}
</script>
</script>
<form name= 'f1 ' id= 'f1 '>
<input id= "all " name= "allcheckbox " type= "checkbox " title= '全选/取消 ' onclick= "cball() "> 全选 <br/>
<input name= "m1 " type= "checkbox " onclick= 'cnall(this) '> <br/>
<input name= "m2 " type= "checkbox " onclick= 'cnall(this) '> <br/>
<input name= "m3 " type= "checkbox " onclick= 'cnall(this) '> <br/>
<input name= "m4 " type= "checkbox " onclick= 'cnall(this) '> <br/>
<input name= "m5 " type= "checkbox " onclick= 'cnall(this) '> <br/>
<input name= "m6 " type= "checkbox " onclick= 'cnall(this) '> <br/>
</form>
</body>