举个简单的例子吧,比如我要做一个考试系统,有20个题,都是单选,我计算分数的时候,希望通过一个大循环一下子算出来,这样如果name相同就比较好处理。但是name相同了以后,所有20个题只能有一个选出来的了,而不是每个题都能选一个了。

解决方案 »

  1.   

    你可以用checkbox替换,如果非要实现你说的办法.我建议这样做
    :
    隐藏两个checkbox
    <script language="javascript">
    function doClick(obj){
      var tarObj;
      if(obj.name=="answer_1"){
         tarObj=document.getElementById("answerCheckbox_1");
         tarObj.value = obj.value;
         tarObj.checked=true;
         return;
      }
      if(obj.name=="answer_1"){
         tarObj=document.getElementById("answerCheckbox_2");
         tarObj.value = obj.value;
         tarObj.checked=true;
         return;
      }}
    </script><html>
      <body>
         <form  name='questionForm'>
          <input type="checkbox" name=answer id="answerCheckbox_1" value="" style="display:none">
          <input type="checkbox" name=answer id="answerCheckbox_2" value="" style="display:none">      <fieldset>
          <input type="radio" name=answer_1 value=1 id=1 onclick="doClick(this)">
          <input type="radio" name=answer_1 value=2 id=1 onclick="doClick(this)">
          <input type="radio" name=answer_1 value=3 id=1 onclick="doClick(this)">
          </fieldset>
          <fieldset>
          <input type="radio" name=answer_2 value=4 id=2 onclick="doClick(this)">
          <input type="radio" name=answer_2 value=5 id=2 onclick="doClick(this)">
          <input type="radio" name=answer_2 value=6 id=2 onclick="doClick(this)">
          </fieldset>
          <input type=button onclick=getRealValue()>
         </form>
      </body>
    </html>
      

  2.   

    为什么不考虑使用id相同,name不同,这样也可以使用相同的id遍历radio啊<html>
      <body>
         <form  name='questionForm'>
          <input type="radio" id=answer value=1 name=1>
          <input type="radio" id=answer value=2 name=1>
          <input type="radio" id=answer value=3 name=1>
          <input type="radio" id=answer value=4 name=2>
          <input type="radio" id=answer value=5 name=2>
          <input type="radio" id=answer value=6 name=2>
          <input type=button onclick=getRealValue()>
         </form>
      </body>
    </html>
      

  3.   

    为什么要这样做呢,改name不就可以吗,或改用CheckBox.
      

  4.   

    <script language="javascript">
    function doClick(obj){
      var tarObj;
      if(obj.name=="answer_1"){
         tarObj=document.getElementById("answerCheckbox_1");
         tarObj.value = obj.value;
         tarObj.checked=true;
         return;
      }
      if(obj.name=="answer_2"){
         tarObj=document.getElementById("answerCheckbox_2");
         tarObj.value = obj.value;
         tarObj.checked=true;
         return;
      }}function getRealValue(){
      tarObj=document.getElementsByName("answer");
      for (var i=0;i<tarObj.length;i++){
        alert(tarObj[i].value);
      }
        
    }
    </script><html>
      <body>
         <form  name='questionForm'>
          <input type="checkbox" name="answer" id="answerCheckbox_1" value="" style="display:none">
          <input type="checkbox" name="answer" id="answerCheckbox_2" value="" style="display:none">      <fieldset>
          <input type="radio" name="answer_1" value=1 id=1 onclick="doClick(this)">
          <input type="radio" name="answer_1" value=2 id=1 onclick="doClick(this)">
          <input type="radio" name="answer_1" value=3 id=1 onclick="doClick(this)">
          </fieldset>
          <fieldset>
          <input type="radio" name="answer_2" value=4 id=2 onclick="doClick(this)">
          <input type="radio" name="answer_2" value=5 id=2 onclick="doClick(this)">
          <input type="radio" name="answer_2" value=6 id=2 onclick="doClick(this)">
          </fieldset>
          <input type=button onclick="getRealValue()">
         </form>
      </body>
    </html>
      

  5.   

    要想复选要么用checkbox要么改名字,一完全可以用checkbox,选择的时候控制让他只选一项.,为何非要用redio?
      

  6.   

    事实上,在我的应用里面原来就有radio,checkbox,id和name的区别意义不大。非常感谢各位!我再好好考虑一下。
      

  7.   

    name设置成"name"+i的形式,最后循环判断结果的时候使用eval("name"+i)取radio对象不就行了
      

  8.   

    参考大家的想法,基本上可以了。因为对javascript不太了解,仍然有几个问题:
    只有通过var obj = document.getElementById,obj才能获得type属性么?(即obj.type有定义)。而通过document.getElementsByName似乎不能。
    只有通过document.getElementsByName才能获得length么?通过document.getElementById似乎不能。
    这是我试验了一下的结果,不知道是不是有这个规律?
    如果要获得value属性呢?等等,又没有比较好的资料?
    这个问题搞定的话,三个帖子同时结,thx so much。
      

  9.   

    document.getElementsByName 取得的是一个数组
    document.getElementById  取得的是一个对象所以getElementsByName才有长度,如果使用type,可以这样document.getElementsByName("name")[0].type
      

  10.   

    var arr = document.getElementsByName("name");
    for(var i=0; i<arr.length; i++)
    {
        if(arr[i].checked)
            alert(arr[i].value);
    }