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.

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(", "));
});

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());

                });                 
            });
$(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

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>

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.