回 帖 发 新 帖 刷新版面

主题:奇怪了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function aa(){
for (var i = 0;i<document.getElementById('club').length-1;i++){
     if(!document.getElementById('club')[i].checked){
     alert("请选择准备与TA。");
    }
    else{
     alert("没有选中");
    }
  }
}
</script>
</head>

<body>
<input type="radio" value="男" name="club" id="club" />男
<input type="radio" value="女" name="club" id="club" />女<br />
<input type="button" onclick="return aa()" value="33333" />
</body>
</html>
单击button,却不执行!!!!为什么?

回复列表 (共2个回复)

沙发

<input type="button" onclick="return aa()" value="33333" />
换成
<input type="submit" onclick="return aa()" value="33333" />

板凳

你这样写是不对的。
document.getElementById('club') 你这个只是获取了一个对象,所以length是不起作用的,这也就是为什么一个页面id值不能出现重复的原因。

你要实现这个功能的话,得这样来写:
function aa(){
    var e = f1.club;
    alert(e.length-1);
    for (var i = 0;i<e.length-1;i++){
         if(e[i].checked){
         alert("请选择准备与TA。");
        }
        else{
         alert("没有选中");
        }
      }
}

<form name="f1">
<input type="radio" value="男" name="club" />男
<input type="radio" value="女" name="club" />女<br />
<input type="button" onclick="return aa()" value="33333" />
</form>

我来回复

您尚未登录,请登录后再回复。点此登录或注册