This article mainly introduces how to dynamically add attributes to an object by using JavaScript. It involves simple skills in JS property traversal, Dynamic assignment, and eval, you can refer to the requirements for dynamically adding attributes to the jd object for your work. You can find a solution on the Internet. The example is as follows:
1. demo
Var aa = "maker"; var bb = 123; var lists = {}; eval ("lists. "+ aa +" = "+ bb); eval ('+" lists. "+ aa +" = "+ bb + '); console. log (lists );
This method is acceptable.
2. Use in projects
$ ("# Saveform "). on ("click", function () {var param = {}; // bz Order No. var bzorderid = $ ("# bzorderid "). val (); // get all products $ ("# allsk. sk td input "). each (function () {var id = this. id; var name = this. name; var val = $ ("#" + id ). val (); // dynamically add attributes and assign values to eval ("param. "+ name +" = "+ val )});}
Retrieve the value through input, and dynamically add attributes and attribute values to the js object.
I hope this article will help you design JavaScript programs.
For more articles about how to dynamically add attributes to an object using JS, refer to PHP!