简体   繁体   中英

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.

You should create a new items[] element for each value that you're adding. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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