简体   繁体   English

如何合并来自不同输入值的数组。

[英]How to merge arrays from different input values.?

There are many text field with same name and same class with different values and each input have 1 button. 有许多文本字段具有相同的名称和相同的类具有不同的值,每个输入有1个按钮。 I get the value from each one of the fields by clicking on each specific button. 我通过单击每个特定按钮从每个字段中获取值。 Using with Javascript. 使用Javascript。 And i can put that value into array but the arrays are not merge. 我可以将该值放入数组,但数组不合并。 If i click on 1st button the specific input field value is put into the array, then i click on 3rd button the 1st value in array is removed and the new vlaue (3rd row's value) is stored in array. 如果我点击第一个按钮,特定的输入字段值被放入数组,然后我点击第三个按钮,数组中的第一个值被删除,新的值(第三行的值)存储在数组中。 How can i store all of the values in same array on each click button. 如何在每个单击按钮上将所有值存储在相同的数组中。

Here is my code. 这是我的代码。

<div>
    <input type="hidden" name="myvalue" class="form-control input_text" value="aaa"/>
    <input type="button" class="clickme" value="Get Value" />
</div>

<div>
    <input type="hidden" name="myvalue" class="form-control input_text" value="bbb"/>
    <input type="button" class="clickme" value="Get Value" />
</div>

<div>
    <input type="hidden" name="myvalue" class="form-control input_text" value="ccc"/>
    <input type="button" class="clickme" value="Get Value" />
</div>

<div>
    <input type="hidden" name="myvalue" class="form-control input_text" value="ddd"/>
    <input type="button" class="clickme" value="Get Value" />
</div>

<div>
    <input type="hidden" name="myvalue" class="form-control input_text" value="eee"/>
    <input type="button" class="clickme" value="Get Value" />
</div>

Here is my javascript 这是我的javascript

<script type="text/javascript">
//Get the value on button click
    var div = $(this).closest('div');
    var get_eupdid = div.find('input').val();

    alert(get_eupdid);

    //Push the value into array.
    var array_id = [];

    array_id.push(get_eupdid);

    console.log( "array_id: " + array_id);
</script>

Try this :Click function not there and declare the array as a global 试试这个 :点击不在那里的函数并将array声明为全局

 var array_id = []; $('.clickme').click(function(){ var get_eupdid= $(this).closest('div').find('input').val(); array_id.push(get_eupdid); console.log( "array_id: " + array_id); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input type="hidden" name="myvalue" class="form-control input_text" value="aaa"/> <input type="button" class="clickme" value="Get Value" /> </div> <div> <input type="hidden" name="myvalue" class="form-control input_text" value="bbb"/> <input type="button" class="clickme" value="Get Value" /> </div> <div> <input type="hidden" name="myvalue" class="form-control input_text" value="ccc"/> <input type="button" class="clickme" value="Get Value" /> </div> <div> <input type="hidden" name="myvalue" class="form-control input_text" value="ddd"/> <input type="button" class="clickme" value="Get Value" /> </div> <div> <input type="hidden" name="myvalue" class="form-control input_text" value="eee"/> <input type="button" class="clickme" value="Get Value" /> </div> 

I think you should check this out. 我想你应该看看这个。 There's usually no need for <input type='hidden' /> when you can use something else to store the data. 当您可以使用其他东西存储数据时,通常不需要<input type='hidden' /> Also, it shows you how, with a little bit of CSS, unless you'd like to put something else in those <div> s, how you can do that. 此外,它还会向您展示如何使用一点CSS,除非您想在这些<div>其他内容,您将如何做到这一点。

 $(function(){ var possible = ['aaa', 'bbb', 'ccc', 'ddd', 'eee'], collection = []; $('.clickme').each(function(i, e){ $(e).click(function(){ collection.push(possible[i]); console.log(collection); }); }); }); 
 .clickme{ display:block; } 
 <head> <script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script> </head> <body> <div> <input type='button' class='clickme' value='Get Value' /> <input type='button' class='clickme' value='Get Value' /> <input type='button' class='clickme' value='Get Value' /> <input type='button' class='clickme' value='Get Value' /> <input type='button' class='clickme' value='Get Value' /> </div> </body> 

You need to make array global.Right now you are creating array every time a button is clicked so only current value remain in array 你需要创建数组global.Right现在你每次单击一个button时都在创建array所以只有current value保留在array

 var array_id = [];
 $(document).ready(function () {
  $(".clickme").click(function () {
     var div = $(this).closest('div');
     var get_eupdid = div.find('input').val();
     alert(get_eupdid);

     //Push the value into array.
      array_id.push(get_eupdid);

      console.log("array_id: " + array_id);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM