This article mainly introduces how javascript uses eval functions to assemble form input items as json objects. The example shows how JavaScript uses eval functions to dynamically construct json objects, for more information about how javascript uses the eval function to assemble form input items as json objects, see the example in this article. We will share this with you for your reference. The details are as follows:
When ajax is used for web development, it is often used to collect form input items before saving them to form a json object, and then directly post the objects to the server.
The general practice is to write code similar to the following in js:
Var myObj = {}; myObj. x = document. getElementById ("x "). value; myObj. y = document. getElementById ("y "). value ;//... // then submit ajax post or get
This is fine when there are not many form elements, but if a form has dozens or even more input items, it is too hard to write this code.
Fortunately, there is an evil eval function in javascript, which can help us do something similar to c # reflection, such as the following:
eval('A={}');if (A.b==undefined){ A.b = {};}eval('A.b.c = 1');alert(A.b.c);
In this way, we dynamically create A composite object A. After understanding the principle, we can make some improvements to the form:
Waybill No:-
Settlement method: Cash End of month
Attributes that do not need to be assigned values: