dton37910
2017-08-10 07:19
浏览 41

jquery数组到字符串以显示文本框输入

im trying to output the arrays associated with the test variable:

            $(document).on('click','#btnSubmit', function(){
                var test = $("input[name*='i_name']");                   
                $(test).each(function(i, item){
                    var sample = [];
                    sample.push($(item).val());

                });                 
            });

and display all the data i got into a textbox:

<input type="text" id="test" name="test[]">

how will i do it it? i tried JSON.stringify, .join but it only displays the last index of the array. please assist me.

图片转代码服务由CSDN问答提供 功能建议

我试图输出与 test 变量关联的数组: \ n

  $(document).on('click','#btnSubmit',function(){
 var test = $(“input [name * ='i_name']”); \  n $(test).each(function(i,item){
 var sample = []; 
 sample.push($(item).val()); 
 
}}; 
})  ; 
   
 
 

并显示我在文本框中输入的所有数据:

 &lt; input type =“text  “id =”test“name =”test []“&gt; 
   
 
 

我将如何做到这一点? 我尝试了 JSON.stringify .join ,但它只显示了数组的最后一个索引。 请帮助我。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duangu9173 2017-08-10 07:21
    已采纳

    You are resetting your sample array in every iteration, remove the variable declaration from the loop.

    $(document).on('click','#btnSubmit', function() {
        var test = $("input[name*='i_name']");
        var sample = [];    // <-- Define sample variable here
        $(test).each(function(i, item){
            sample.push($(item).val());
        });
    
        console.log(sample.join(", "));
    });
    
    打赏 评论
  • doufu5401 2017-08-10 07:22

    Declare the sample array in global variable of click function, not with each function .Because each time loop will be reset the array.That why its have only last element value

     $(document).on('click','#btnSubmit', function(){
              var sample = [];
                    var test = $("input[name*='i_name']");                   
                    $(test).each(function(i, item){
                        sample.push($(item).val());
    
                    });                 
                });
    
    打赏 评论
  • douzai3399 2017-08-10 07:22
    $(document).on('click','#btnSubmit', function(){
                    var sample = [];
                    var test = $("input[name*='i_name']");                   
                    test.each(function(i, item){
    
                        sample.push($(item).val());
    
                    });                 
                });
    

    I think.. I should change $(test) => test

    打赏 评论
  • dongshao1021 2017-08-10 07:24

    You can output the array to the input box by just updating the value.

    In your code, your sample variable is getting updated to empty array [] each time, hence it is storing only the latest value.

    const text = ['1','2','3','4','5'];
    
    $("#test").val(text.join(''));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
    <input type="text" id="test" />

    </div>
    
    打赏 评论
  • doucai4274 2017-08-10 07:33

    Two main mistakes, you are clearing the array each time inside the loop, declare var sample = [], globally, seconding using the wrong selector, directly use the jquery object than assigning to a variable and using it.

     $(document).on('click','#btnSubmit', function(){
                          var sample = [];             
                    $("input[name*='i_name']").each(function(i, item){
                        sample.push($(item).val());
                    });
                    console.log(sample.toString()); // it will convert to string.
                });
    

    Hope it helps.

    打赏 评论

相关推荐 更多相似问题