繁体   English   中英

Javascript获取输入框数组的值

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

我有一个输入字段,该字段具有一个按钮,可以动态添加另一个输入字段,而我尝试获取它,以便在单击plot时,我能够在输入字段gps[]获取内容。

HTML

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

情节

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

您必须使用.map来获取集合中所有元素的值:

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

小提琴

暂无
暂无

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

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