html代码如下:<input type="text" id="txt1" >
<input type="text" id="txt2" >
<input type="text" id="txt3" >
<input type="text" id="txt4" >
<input type="text" id="txt5" >
<input type="text" id="txt6" ><!-- 隐藏域放所有值!-->
<input type="hidden" id="allinfo" >
且text还可以动态添加!
问题是如何获得所有text值然后放到 allinfo 好传到后台中!
谢谢请大家帮忙!

解决方案 »

  1.   

    var i = 0;
    var content = "";
    while(document.getElementById("txt" + i)) {
        content += document.getElementById("txt" + i);
    }//然后赋值、传值
      

  2.   


    <script>function doit(){
      var f = document.forms[0];
      var s = "";
      for(var i=0;i<f.elements.length;i++){
         if(f.elements[i].type != "text") continue;
         if(f.elements[i].id.substr(0,3) != "txt") continue;
         if(f.elements[i].value=="") continue;
         s += f.elements[i].value + ",";
      }
      if(s!="") s =s.substr(0,s.length-1);
      alert(s);
      f.allinfo.value = s;
    }
    </script>
    <form>
    <input type="text" id="txt1" >
    <input type="text" id="txt2" >
    <input type="text" id="txt3" >
    <input type="text" id="txt4" >
    <input type="text" id="txt5" >
    <input type="text" id="txt6" >
    <input type="button" value="click" onclick="doit()">
    <!-- 隐藏域放所有值!-->
    <input type="hidden" id="allinfo" >
    </form>
      

  3.   

    动态添加的代码上面已经有了
    设置相同的name属性即可获取到各个文本框的内容
      

  4.   

    我用的是jquery写的,所以你要下一个jquery的类库,代码如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title></title>
    </head>
    <script type="text/javascript" src="wsn_global_test.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    var i=0;
    $("#addBtn").click(function() {
    i++;
    $(this).after("<br /><input type='text' id='txt" + i + "' /><br />");
    });

    $("#showBtn").click(function() {
    if($("input[id^='txt']").length > 0) {
    var html = "";
    $("input[id^='txt']").each(function() {
    if($(this).val()) {
    html += $(this).val() + ";";
    }
    });
    $("#allinfo").val(html);
    }
    });
    });
    </script>
        
    <body>

    <a id="addBtn" href="javascript:void(0);">add it</a>
    <input type="hidden" id="allinfo" />
    <br />
    <input type="button" value="show all value" id="showBtn" />

    </body>
    </html>
      

  5.   

    "wsn_global_test.js"是我下的jquery的类库,然后重新改的名字,你可以下一个
      

  6.   


    如果我上面还有一个需要单独提交的控件呢?
    比如:<form>
    <!-- 单独的-->
    <input type="text" id="others" />
    <!-- 单独的-->
    <input type="text" id="txt1" >
    <input type="text" id="txt2" >
    <input type="text" id="txt3" >
    <input type="text" id="txt4" >
    <input type="text" id="txt5" >
    <input type="text" id="txt6" >
    <input type="button" value="click" onclick="doit()">
    <!-- 隐藏域放所有值!-->
    <input type="hidden" id="allinfo" >
      

  7.   

    <html>
    <body>
    <input type="text" id="txt1" value="111" />
    <input type="text" id="txt2" value="222" />
    <input type="text" id="txt3" value="333" />
    <input type="text" id="txt4" >
    <input type="text" id="txt5" >
    <input type="text" id="txt6" >

    <!-- 隐藏域放所有值!-->
    <input type="hidden" id="allinfo" >

    </body>
    <script>
    getAllInfo();
    function getAllInfo(){

    var allObj = document.getElementsByTagName("input");
    for(var i=0;i<allObj.length;i++){
    if(allObj[i].type == "text"){
    if(allObj[i].value != null && allObj[i].value != ""){

    document.getElementById("allinfo").value += allObj[i].value;
    document.getElementById("allinfo").value += ",";
    } }
    }
    alert(document.getElementById("allinfo").value);
    }
    </script>
    </html>
      

  8.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>无标题页</title>
         <script type="text/javascript">
        function ShowInfo()
        {
          var Text=document.getElementsByTagName("input");
          var Array="";
            for(var i=0;i<Text.length;i++)
            {
                if(Text[i].type=="text")
                {
                  Array+=",";
                  Array+=Text[i].value;
                }
               
            }
           Array=Array.substring(1,Array.length-2);
           document.getElementById('Hidden1').value=Array;
           alert(document.getElementById('Hidden1').value);
          }
        </script>
    </head>
    <body>
      <input type="text" id="txt1" ><br />
    <input type="text" id="txt2" ><br />
    <input type="text" id="txt3" ><br />
    <input type="text" id="txt4" ><br />
    <input type="text" id="txt5" ><br />
    <input type="text" id="txt6" ><br />
        <input id="Hidden1" type="hidden" />
            <input id="Button1" type="button" value="提交" onclick="ShowInfo()" />
    </body>
    </html>
      

  9.   


    <script>
    function getvalue(){
      var a=[];
      var o=document.getElementById("czp").getElementsByTagName("INPUT");  for(var i=0;i<o.length;i++){
        a.push(o[i].value);
      }
      document.getElementById("czp").value=a.join(',')
      //alert(document.getElementById("czp").value)
    }
    </script>
    <!-- 单独的-->
    <input type="text" id="others" /><div id=czp>
    <input type="text" id="txt1" />
    <input type="text" id="txt2" />
    <input type="text" id="txt3" />
    <input type="text" id="txt4" />
    <input type="text" id="txt5" />
    <input type="text" id="txt6" />
    </div>
    <br>
    <input type="button" value="test" onclick="getvalue()"/><!-- 隐藏域放所有值!-->
    <input type="hidden" id="allinfo" >
      

  10.   

    要点是:DIV(id=czp)中只放隐藏域要用的text
      

  11.   

    [code]
    var o,t='';
    for(var i=0;o=document.getElementById('txt'+i);i++){
        t+=o.value;
    }
    document.getElementById('allinfo').value=t;
    [/code]
      

  12.   

    没你这做法的 用 设置个name就可以取到集合了 
      

  13.   

    还是2楼写的最优雅,
    6楼用jQuery可以将结构和行为更好地分离
      

  14.   

    $("input[@type=text]").each(
    function(i,v){
        $("hidden").val(v.val());
    }
    )简单明了的JQ
      

  15.   


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script language="JavaScript">
    window.onload = function(){
    var $ = function(id){
    return document.getElementById(id);
    };
    var oInputs = document.getElementsByTagName('input');
    var Result = "";
    $('click').onclick = function(){
    for (var i = 0; i < oInputs.length; i++) {
    (function(obj){
    if (obj.type == 'text' && obj.value != "" ) {
    Result += obj.value!=""?obj.value:"";
    Result += "|";
    }
    })(oInputs[i])
    }

    $('allinfo').value = Result;
    alert($('allinfo').value);
    };

    };
    </script>
    </head>
    <body>
    <form>
    <!-- 单独的-->
    <input type="text" id="others" />
    <!-- 单独的-->
    <input type="text" id="txt1" >
    <input type="text" id="txt2" >
    <input type="text" id="txt3" >
    <input type="text" id="txt4" >
    <input type="text" id="txt5" >
    <input type="text" id="txt6" >
    <input type="button" value="click" id="click">
    <!-- 隐藏域放所有值!-->
    <input type="hidden" id="allinfo" >
    </form>
    </body>
    </html>楼主看看~·