简体   繁体   English

使用jQuery设置输入数组值

[英]Set Input array value using jquery

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. 使用表单提交上的jQuery进入input array

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设置输入数组的值?

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

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

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