表单每一组项目中有两个相同name的select:<form name="form1" id="form1">
<select name="a1" id="a1"><option value="0"></option></select><select name="a1" id="a1"><option value="1"></option></select><input name="count" type="button" onclick="chkform()" /><select name="a2" id="a2"><option value="1"></option></select><select name="a2" id="a2"><option value="0"></option></select><input name="count" type="button" onclick="chkform()" />……<select name="an" id="an"><option value="0"></option></select><select name="an" id="an"><option value="1"></option></select><input name="count" type="button" onclick="chkform()" /></form>每组后面的button触发chkform的时候,如何统计表单中有相同name的select的值?使其alert出来的结果用逗号隔开,如点击a1后面的button,得到a1的结果为:0,1  ;点击a2后的button,得到结果为1,0,一直循环下去?也就是说chkform这个js函数应该怎么写呢?

解决方案 »

  1.   

    使用document.getElementById("a1").value这样似乎只能得到每组的第一个select值,如何构造循环使其每次点击统计的时候,统计这一组所有的同名select的值?使其结果类似asp接受表单时自动将同名的值变成0,1,2这样的形式?
      

  2.   

    var selects=document.getElementsByName("");
    var len=selects.length;
    if(len>0){
      for(var i=0;i<len;i++){
          alert(selects[i].value);
      } 
    }
      

  3.   


        <select name="a1" id="a1">
            <option value="0"></option>
        </select><select name="a1" id="a1"><option value="1"></option>
        </select><input name="count" type="button" onclick="chkform()" />    <script type="text/javascript">
            function chkform() {
                var sel = document.getElementsByName("a1");
                var val = "";
                for (var i = 0, len = sel.length; i < len; i++) {
                    val += ","+sel[i].value;
                }
                if (val.length > 0) {
                    val = val.substring(1);
                }
                alert(val);
            }
        </script>
      

  4.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> New Document </title>
    <meta name="Generator" content="EditPlus">
    <meta name="Author" content="">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    </head><body>
    <script language="JavaScript">
    <!--
    function chkform(name){
    var str ="";
    var objs = document.getElementsByName(name) ;
    for(var i=0;i<objs.length ;i++){
    alert(objs[i].type);
    if(objs[i].type.indexOf("select")>-1){
    for(var n=0 ; n<objs[i].options.length ;n++){
    if(objs[i].options[n].selected)
    str += objs[i].options[n].value +",";
    }
    }
    }
    alert(str.substring(0,str.length-1)) ;
    }
    //-->
    </script>
    <form name="form1" id="form1">
    <select name="a1" id="id1">
    <option value="0">00000</option>
    </select><select name="a1" id="id2">
    <option value="1">11111</option>
    </select>
    <input name="count" type="button" onclick="chkform('a1')" value="得到a1的值"/><select name="a2" id="id3">
    <option value="aaa">aaa</option>
    </select><select name="a2" id="id4">
    <option value="bbb">bbbb</option>
    </select>
    <input name="count" type="button" onclick="chkform('a2')" value="得到a2的值"/>
    ...........
    </form></body>
    </html>