<html><body><script type="text/javascript">
function check(){
  var ck=document.getElementsById('check');
  var isSel=false;
for(var i=0;i<ck.length;i++)
{
  if(ck[i].checked==true)
   {
    isSel=true;
    break;
   }
}
if(isSel==false)
{
alert("请选择兴趣爱好!"); 
return false;
}else
{
return true;
}</script><form onsubmit="return check()">兴趣爱好:
<br/>
唱歌:
<input type="checkbox" name="singing" id="check">
<br/>
爬山:
<input type="checkbox" name="climbing" id="check">
<br/>
写作:
<input type="checkbox" name="writing" id="check">
<br/>
游泳:
<input type="checkbox" name="swiming" id="check">
<br/>
跑步:
<input type="checkbox" name="running" id="check">
<br/>
<br/><input type="submit" name="tj" value="注册" />
</form></body>
</html>我只是想检测复选框是否有选择,如果没有就弹出警告框。但是运行起来没反应,不知道是哪里出了问题。

解决方案 »

  1.   

    <html> 
    <body>
    <script type="text/javascript">
    function checkk(){
    var ck = document.getElementsByTagName('input');
    var isSel = false;
    for (var i = 0; i < ck.length; i++) {
    if (ck[i].checked == true) {
    isSel = true;
    break;
    }
    }
    if (isSel == false) {
    alert("请选择兴趣爱好!");
    return false;
    } else {
    return true;
    }
    }</script> 
    <form onsubmit="return checkk()">兴趣爱好: 
    <br /> 唱歌: 
    <input id="check" name="singing" type="checkbox"> 
    <br /> 爬山: 
    <input id="check" name="climbing" type="checkbox"> 
    <br /> 写作: 
    <input id="check" name="writing" type="checkbox"> 
    <br /> 游泳: 
    <input id="check" name="swiming" type="checkbox"> 
    <br /> 跑步: 
    <input id="check" name="running" type="checkbox"> 
    <br /> 
    <br /> 
    <input name="tj" type="submit" value="注册" /> 
    </form>
    </body>
    </html> 
      

  2.   

    呵呵应该是id不允许重复吧,2楼正解!
    document.getElementsById这个获取的不是一个集合吧。应该是单个元素