简体   繁体   中英

How to create input elements dynamically from an array with jQuery?

I am creating input elements dynamically. I have an array that has n number of elements. I need to create n input boxes using Jquery and put the value of each element of an array into a separate input box.

I am using the .trigger("click") function to simulate a click. Additionally, I tried looking for solution, but could not find a satisfactory result. Therefore, I decided to put this question. I looked at .map() Jquery function. The .map().get function returns the array from dynamically created input boxes.

 var values = [1, "hello", 1.6, "some other value"]; // the container you want to append the inputs to var $container = $("#container"); values.forEach(function(value) { // create the an input var $input = $("<input/>"); // change it's properties (if you want to) // $input.addClass("someClass"); // $input.attr("name", "someName"); // $input.attr("id", "someID"); // ... // set its value $input.val(value); // append it to the container $container.append($input); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"></div>

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