简体   繁体   English

为输入数组赋值

[英]assigning values to input array

if I have this:如果我有这个:

 function add(){ var val = document.getElementById("item").value; document.getElementById("items").value = val; }
 <table> <input type="text" name="item" id="item" value="" > <input type="hidden" name="items[]" id="items[]" value="" > <button onclick="add()" type="button"> Add</button> </table>

so I just want add some values to input items[], It's possible?所以我只想在输入项[]中添加一些值,这可能吗? in javascript I use push(), but in this case I don't know what to do.在 javascript 中我使用 push(),但在这种情况下我不知道该怎么做。

You should create a new items[] element for each value that you're adding.您应该为要添加的每个值创建一个新的items[]元素。 Then the middleware on the server should automatically create an array with all these values.然后服务器上的中间件应该自动创建一个包含所有这些值的数组。

 function add(){ var val = document.getElementById("item").value; var item = document.createElement("input"); item.type = "hidden"; item.name = "items[]"; item.value = val; document.getElementById("form").appendChild(item); }
 <form id="form"> <input type="text" name="item" id="item" value=""> <button onclick="add()" type="button"> Add</button> </form>

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

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