我正在尝试输出与 test
变量关联的数组:
$(document).on('click','#btnSubmit', function(){
var test = $("input[name*='i_name']");
$(test).each(function(i, item){
var sample = [];
sample.push($(item).val());
});
});
并显示我进入文本框的所有数据:
<input type="text" id="test" name="test[]">
我要怎么做呢?我尝试了 JSON.stringify
、.join
但它只显示数组的最后一个索引。请帮助我。
最佳答案
您将在每次迭代中重置示例数组,从循环中删除变量声明。
$(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(", "));
});
关于javascript - jquery数组到字符串以显示文本框输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45606967/