js判断checkbox选中

<script type="text/javascript"> 
function check_box() { 
var count=document.myform.checkThis.length; 
//var count=document.getElementById('checkThis').length;
alert(count);
var j=0; 
for(var i=0;i<count;i++) 
{ 
if (document.myform.checkThis[i].checked) 
//if (document.myform.checkThis[i].checked) 
//document.getElementById('checkThis').checked;
++j; 
} 
if(j==0) {
alert("至少选择一个!!"); 
return false;
} 
else 
{ 
return true;
} 
}
</script> 

<form name="myform" action="" onsubmit="return check_box();"> 
<input type="checkbox" name="checkThis" value="1">1<br> 
<input type="checkbox" name="checkThis" value="2">2<br> 
<input type="checkbox" name="checkThis" value="3">3<br> 
<input type="submit" value="提交" > 
</form> 

Leave a Reply

(will not be published)