我有一个输入字段,其中有一个按钮可以动态添加另一个输入字段,我正在尝试获取它,以便当我单击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();
关于Javascript 获取输入框数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21077744/