javascript 使用 jquery 设置输入数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20186377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Input array value using jquery
提问by Runcorn
I am trying to implement html input array.
我正在尝试实现 html输入数组。
<input type="text" name="firstName[]" id="firstName[]">
And i need to set value of another form which looks something like
我需要设置另一种形式的值,它看起来像
<form id="tempForm">
<input type="text" name="userName" id="userName">
<input type="text" name="userId" id="userId">
</form>
into the input array
using jquery on form submit.
进入input array
上形式使用jQuery提交。
For that i tried following on form submit,
为此,我尝试按照表单提交,
var currentIndex=$("input[name^=firstName]").length;
$("#firstName").eq(currentIndex).val($("#userName").val());
But it doesn't works,obviously.
但它显然不起作用。
Question:
问题:
How to set value of input array using jquery?
如何使用jquery设置输入数组的值?
回答by Vahid Taghizadeh
Use the jquery append function for add inputs with different attribute value : Check it :
使用 jquery append 函数添加具有不同属性值的输入:检查它:
$(document).ready(function(){
var a = ["username","userid"];
var b = ["username","userid"];
for( var i = ; i <3 ; i++){
$('#tempForm').append('<input type="text" name="'+a[i]+'" id="'+b[i]+'" />);
}
});
Then continue your other work:
然后继续你的其他工作:
replace this code with your js code :
将此代码替换为您的 js 代码:
var currentIndex=$("input[name^=firstName]").length;
$("#firstName").eq(currentIndex).val($("#"+userName).val());