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

检查radio是否全选
做一个问卷系统,一个问卷至少有100个问题,每个问题都是单选,但要求问卷的所有问题都必须回答,也就是把所有的radio都要选上,否则不让提交,那么如何用实现检查radio是否全选的功能呢?

------解决方案--------------------
例:已测试!!
<input name="chk" type="checkbox"/>
<input name="chk" type="checkbox"/>
<input name="chk" type="checkbox"/>
<input name="chk" type="checkbox"/>
<input type="button" onclick="check();"/>

function check(){
var chkObj = document.getElementsByName("chk");
var selCount=0;
alert(chkObj.length);
for(var i=0;i<chkObj.length;i++){
if(chkObj[i].checked==true){
selCount++;
}
}
if(selCount==chkObj.length){alert("已经全选!");}else{alert("未全选!");}
}
------解决方案--------------------
HTML code

<form action="http://www.csdn.net" onsubmit=" return  checkall();"   >

<div class="subject">
<h2>你喜欢吃的水果?</h2>
 <input type="radio" name="fruit" value="苹果" id="Radio1" /><label for="Radio1">苹果</label>
<input type="radio" name="fruit" value="香蕉" id="Radio2" /><label for="Radio2">香蕉</label>
<input type="radio" name="fruit" value="桔子" id="Radio3" /><label for="Radio3">桔子</label>
<input type="radio" name="fruit" value="梨" id="Radio4" /><label for="Radio3">梨</label>
</div>

<div class="subject">
<h2>你喜欢的颜色?</h2>
 <input type="radio" name="color" value="红" id="Radio5" /><label for="Radio5">红</label>
<input type="radio" name="color" value="蓝" id="Radio6" /><label for="Radio6">蓝</label>
<input type="radio" name="color" value="绿" id="Radio7" /><label for="Radio7">绿</label>
<input type="radio" name="color" value="紫" id="Radio8" /><label for="Radio8">紫</label>
</div>

<div class="subject">
<h2>你想去旅游的国家?</h2>
 <input type="radio" name="state" value="日本" id="Radio9" /><label for="Radio9">日本</label>
<input type="radio" name="state" value="美国" id="Radio10" /><label for="Radio10">美国</label>
<input type="radio" name="state" value="德国" id="Radio11" /><label for="Radio11">德国</label>
<input type="radio" name="state" value="英国" id="Radio12" /><label for="Radio12">英国</label>
</div>
<input type="submit" value="提交"   />
</form>
  <script type="text/javascript" >
      function checkall() {
          var divs = document.getElementsByTagName("DIV");
          var ischeckall = true,ischeckone=false;
          for (var i = 0; i < divs.length; i++) {
              if (divs[i].className == "subject") {
                  var radios = divs[i].getElementsByTagName("input");
                  ischeckone = false;
                  for (var j = 0; j < radios.length; j++) {
                      if (radios[j].checked) { ischeckone = true; break; }
                  }
                  if (!ischeckone) {
                      alert("您这一题