<html>
<head>
<Script>
//我想提交的时候,能先进行判断下有没有选择复选框,一个都没选,则弹出对话框,现在程序有问题,运行什么也没做,请大家帮忙看看错误处在哪里了????谢谢大家乐。
function check (){
var num=document.form1.checkbox.length;
for(var i=0;i<num;i++){
if(!(form1.checkbox[i].checked))    
   alert("至少选择一个");
   return false;
  else{
  return true;
  }
 }
}
</script>
</head>
<body >
<form name="form1" onSubmit="return check()" action="bb.htm">
<input type="checkbox" name="checkbox" value="aa">唱歌
<input type="checkbox" name="checkbox" value="bb">跳舞
<input type="checkbox" name="checkbox" value="cc">上网
<input type="checkbox" name="checkbox" value="dd">泡吧
<input type="checkbox" name="checkbox" value="ee">聊天
<input type="submit" value="submit">
</form>
</body>
</html>

解决方案 »

  1.   


    <html> 
    <head> 
    <Script> 
    function check(){ 
      var a =document.form1.checkbox;
      n = 0;
      for(var i=0;i<a.length;i++){ 
        if(a[i].checked) n++;
      }
      if(n==0){
        alert("至少选择一个"); 
         return false;
      }
      else return true; 

    </script> 
    </head> 
    <body > 
    <form name="form1" onSubmit="return check()" action="bb.htm"> 
    <input type="checkbox" name="checkbox" value="aa">唱歌 
    <input type="checkbox" name="checkbox" value="bb">跳舞 
    <input type="checkbox" name="checkbox" value="cc">上网 
    <input type="checkbox" name="checkbox" value="dd">泡吧 
    <input type="checkbox" name="checkbox" value="ee">聊天 
    <input type="submit" value="submit"> 
    </form> 
    </body> 
    </html>
      

  2.   


    <html> 
    <head> 
    <Script> 
    //我想提交的时候,能先进行判断下有没有选择复选框,一个都没选,则弹出对话框,现在程序有问题,运行什么也没做,请大家帮忙看看错误处在哪里了????谢谢大家乐。 
    function check(){
    var isBound = false;
    var ock = document.getElementsByName("checkbox");
    for(var i = 0, len = ock.length; i < len; i++){
    if(ock[i].checked){
    isBound = true;
    }
    }
    if(!isBound){
    alert("至少选择一个!");
    }
    return isBound;
    }
    </script> 
    </head> 
    <body > 
    <form name="form1" onSubmit="return check()" action="bb.htm"> 
    <input type="checkbox" name="checkbox" value="aa">唱歌 
    <input type="checkbox" name="checkbox" value="bb">跳舞 
    <input type="checkbox" name="checkbox" value="cc">上网 
    <input type="checkbox" name="checkbox" value="dd">泡吧 
    <input type="checkbox" name="checkbox" value="ee">聊天 
    <input type="submit" value="submit"> 
    </form> 
    </body> 
    </html>
      

  3.   


    function check (){ 
    var num=document.form1.checkbox.length;  for(var i=0;i <num;i++){ 
    if((form1.checkbox[i].checked)){
    return true; 
    }

    alert('至少选择一个');
    return false;
    } lz仔细推敲一下就知道自己的逻辑错误了