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

解决方案 »

  1.   

    例:已测试!!
    <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("未全选!");}
    }
      

  2.   

    要对100组以上的radio判断,怎么办
      

  3.   

    多组:
    <input name="rdo_1" type="radio"/>
    <input name="rdo_1" type="radio"/>
    <input name="rdo_1" type="radio"/>
    <input name="rdo_1" type="radio"/>
    <br/>
    <input name="rdo_2" type="radio"/>
    <input name="rdo_2" type="radio"/>
    <input name="rdo_2" type="radio"/>
    <input name="rdo_2" type="radio"/>
    <br/>
    <input name="rdo_3" type="radio"/>
    <input name="rdo_3" type="radio"/>
    <input name="rdo_3" type="radio"/>
    <input name="rdo_3" type="radio"/><input type="button" onclick="check(); />
    js如下:
    function check(){
      var size=3;//radio组数
      var checkedCount=0;
      for(var i=1;i<=size;i++){
        var rdoObj = document.getElementsByName("rdo_"+i);//按钮命名需有规律
        for(var j=0;j<rdoObj.length;j++){
            if(rdoObj[j].checked==true){
              checkedCount++;
            }
        }
      }
      if(checkedCount==size){alert("已经全选!");}else{alert("未全选!");}
    }
      

  4.   


    <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("您这一题还没有选择:" + divs[i].getElementsByTagName("H2")[0].innerHTML);
                          return false;
                       } 
                   }
               }
               return true;
          }
      </script>如果用jquery 可以写的更简洁一些,不过原理上是相同的