日期:2014-05-16  浏览次数:20424 次

求正确的radio个数算法!为什么算radio个数是1的时候,出错:underfined
当radio个数是1,下列算法会出错:underfined

求正确的radio个数算法

JScript code

<script>
function cnt() {                                        
  //var radioCount = document.all.item("pay_class").length;    
  var radioCount = document.baseinfo.pay_class.length;
  alert("radioCount: " + radioCount);
}
</script>
</head>

<body>
<form name="baseinfo" id="baseinfo" method="post">
  <input type="radio" name="pay_class" id="pay_class" onclick="cnt()">MyRadio<br/>    
</form>
</body>





------解决方案--------------------
通过radio的name属性
JScript code
function cnt() {                                        

  var radioCount = document.getElementsByName("pay_class").length;
  alert("radioCount: " + radioCount);
}