Javascript uses the eval function to assemble a json object as the form input.

Source: Internet
Author: User

Javascript uses the eval function to assemble a json object as the form input.

This example describes how javascript uses eval functions to assemble form input items as json objects. 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: <input type = "text" name = "AwbPre" value = "112" style = "width: 40px "/>-<input type =" text "name =" AwbNo "value =" 12312311 "/> <br/> settlement method: <select name = "SettlementMode" style = "width: 100px "> <option value =" CASH "selected =" selected "> CASH </option> <option value =" MONTH "> monthly settlement </option> </select> <br/> attributes that do not need to be assigned values: <input type = "input" name = "NotMe" value = "NotMe... "isModel =" false "/> <script type =" text/javascript "> function set FormModel (modelName) {eval (modelName + "= {}"); var inputArr = document. getElementsByTagName ("INPUT"); for (var I = 0; I <inputArr. length; I ++) {var isModel = inputArr [I]. getAttribute ("isModel"); var itemName = inputArr [I]. name; var itemValue = inputArr [I]. value; if (isModel! = "False") {eval (modelName + ". "+ itemName +" = '"+ itemValue +"'; ") ;}} var selectArr = document. getElementsByTagName ("SELECT"); for (var I = 0; I <selectArr. length; I ++) {var isModel = selectArr [I]. getAttribute ("isModel"); var itemName = selectArr [I]. name; var itemValue = selectArr [I]. value; if (isModel! = "False") {eval (modelName + ". "+ itemName +" = '"+ itemValue +"'; ") ;}} return modelName;} setFormModel (" AwbModel "); alert (" Ticket No.: "+ AwbModel. awbPre + "-" + AwbModel. awbNo + "\ n settlement method:" + AwbModel. settlementMode + "\ n attributes not available:" + AwbModel. notMe); </script>

In this way, as long as the name attribute of the form element is correctly set and the form object needs to be collected, call the setFormModel function to quickly obtain a json object (of course, this is just an example, only the first-level attribute is processed. If there are multiple-level attributes, you can expand them by yourself. This is nothing more than making some articles on strings)

I hope this article will help you design JavaScript programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.