简体   繁体   English

Javascript获取输入框数组的值

[英]Javascript getting the values of an array of input boxes

I have an input field that has a button to dynamically add another input field and I am trying to get it so that when I click plot i am able to grab the content inside the input fields gps[] 我有一个输入字段,该字段具有一个按钮,可以动态添加另一个输入字段,而我尝试获取它,以便在单击plot时,我能够在输入字段gps[]获取内容。

html HTML

<div id="marker">
    <input type="text" name="gps[]" id="gps">
    <input type="text" name="gps[]">
</div>

Plot 情节

javascript JavaScript的

var counter = 1;
var limit = 3;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<input type='text' name='gps[]'>";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}
$('body').on('click','.plot', function(){
    var y = $('input[name="gps[]"]').val();
    console.log(y);
});

You have to use .map to get the value of all the elements in the collection : 您必须使用.map来获取集合中所有元素的值:

var y = $('input[name="gps[]"]').map(function() {return this.value;}).get();

FIDDLE 小提琴

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

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