我想使用name =“Item [fields] [x]”收集作为数组归档的输入列表。 我的表格:
<form id="form2" name="form2" method="post" action="">
<input id="player_name661" type="text" name="Item[fields][1]" value=""><a href="#" id="clone1" name="dup1" >Use same</a><br />
<input id="player_name662" type="text" name="Item[fields][2]" value=""><a href="#" id="clone2" name="dup2" >Use same</a><br />
<input id="player_name663" type="text" name="Item[fields][3]" value=""><a href="#" id="clone3" name="dup3" >Use same</a><br />
<input id="player_name664" type="text" name="Item[fields][4]" value=""><br />
<input id="player_name665" type="text" name="Item[fields][5]" value=""><br />
....
<input id="player_name66x"" type="text" name="Item[fields][x]" >
</form>
</form>
jquery:
for ($i=1; i < (Item[fields].length+1)/3; $i++) {
var myinput = $("Item[fields][1]").val();
$("input[name^='fields][1+3(i)]").val(myinput);
}
更改以3的倍数完成。 注意:我只能使用name属性,没有id或class。所以点击:
dup1, clone field[1] into [4],[7] ...[1+3(i)]
dup2, clone field[2] into [5],[8],...[2+3(i)]
dup3, clone field[3] into [6],[9],...[3+3(i)]
答案 0 :(得分:3)
试试这个:
var all_inputs = $("input[name^=Item\\[fields\\]]");
for (i = 0; i < all_inputs.length; i++) {
if (i % 3 == '0') {
var val = all_inputs.eq(i).val(); //get the new value for each group of 3
}
all_inputs.eq(i).val(val); //assign the value to inputs in this group of 3
}
演示here