Interaction between JavaScript form fields and JSON data 1th/3 page _json

Source: Internet
Author: User
Tags json
Includes collection properties in objects, referencing other object properties in objects:
Copy Code code as follows:

/**
**json object data to form fields
*/
function Jsonobjecttoform (form, jsonobject) {
for (i = 0, max = Form.elements.length i < max; i++) {
e = Form.elements[i];
ename = E.name;
if (Ename.indexof ('. ') > 0) {
Dotindex = Ename.indexof ('. ');
ParentName = ename.substring (0, Dotindex);
ChildName = ename.substring (dotindex+1);
Iterative judgment ename, assembled into a JSON data structure
Evalue = Itervaluefromjsonobject (Jsonobject, ParentName, childname);
}else{
Evalue = Jsonobject[ename];
}
if (evalue && evalue!= "undefined" && evalue!= "null") {
Switch (e.type) {
Case ' checkbox ':
Case ' Radio ':
if (E.value = = Evalue) {
E.checked = true;
}
Break
Case ' hidden ':
Case ' password ':
Case ' textarea ':
Case ' text ':
E.value = Evalue;
Break
Case ' Select-one ':
Case ' select-multiple ':
for (j = 0; J < E.options.length; J + +) {
op = e.options[j];
Alert ("ename:" + ename + "; Op value: "+ Op.value +"; Evalue: "+ evalue);
if (Op.value = = Evalue) {
Op.selected = true;
}
}
Break
Case ' button ':
Case ' file ':
Case ' image ':
Case ' Reset ':
Case ' Submit ':
Default
}
}
}
}

/**
* There are two ways to read and write JSON arrays
* 1:a.bs[0].id
* 2:a["BS"][0]["id"]
* Convert form to JSON data format
*/
function Formtojsonobject (form) {
var jsonobject = {};
for (i = 0, max = Form.elements.length i < max; i++) {
e = Form.elements[i];
EM = new Array ();
if (E.type = = ' Select-multiple ') {
for (j = 0; J < E.options.length; J + +) {
op = e.options[j];
if (op.selected) {
Em[em.length] = Op.value;
}
}
}
Switch (e.type) {
Case ' checkbox ':
Case ' Radio ':
if (!e.checked) {break;}
Case ' hidden ':
Case ' password ':
Case ' Select-one ':
Case ' select-multiple ':
Case ' textarea ':
Case ' text ':
ename = E.name;
if (E.type = = ' Select-multiple ') {
Evalue = em;
}else{
Evalue = E.value.replace (New RegExp (' ([\\\\]) ', ' G '), ' \\$1 ');
}
Determine if it is an object type data
if (Ename.indexof ('. ') > 0) {
Dotindex = Ename.indexof ('. ');
ParentName = ename.substring (0, Dotindex);
ChildName = ename.substring (dotindex+1);
Iterative judgment ename, assembled into a JSON data structure
Iterjsonobject (Jsonobject, ParentName, ChildName, Evalue);
}else{
Jsonobject[ename] = Evalue;
}
Break
Case ' button ':
Case ' file ':
Case ' image ':
Case ' Reset ':
Case ' Submit ':
Default
}
}
return jsonobject;
}

/**
* Convert form element iterations to JSON data
*/
function Iterjsonobject (Jsonobject, ParentName, ChildName, Evalue) {
Parrayindex is used to determine whether an element is an array label
Parrayindex = Parentname.indexof (' [');
Determines whether the data is aggregated, not just the object property
if (Parrayindex < 0) {
var child = Jsonobject[parentname];
if (!child) {
Jsonobject[parentname] = {};
}
Dotindex = Childname.indexof ('. ');
if (Dotindex > 0) {
Iterjsonobject (Jsonobject[parentname], childname.substring (0, Dotindex), childname.substring (dotIndex+1), eValue);
}else{
Jsonobject[parentname][childname] = Evalue;
}
}else{
Parray = jsonobject[parentname.substring (0, Parrayindex)];
Initializes an array type if no JS array exists
if (!parray) {
Jsonobject[parentname.substring (0, parrayindex)] = new Array ();
}
Get the set subscript, and determine if the corresponding subscript exists JS object
arrayindex = parentname.substring (parrayindex+1, parentname.length-1);
var c = jsonobject[parentname.substring (0, Parrayindex)][arrayindex];
if (!c) {
Jsonobject[parentname.substring (0, parrayindex)][arrayindex] = {};
}
Dotindex = Childname.indexof ('. ');
if (Dotindex > 0) {
Iterjsonobject (jsonobject[parentname.substring (0, Parrayindex)][arrayindex], childname.substring (0, DotIndex), Childname.substring (dotindex+1), evalue);
}else{
Jsonobject[parentname.substring (0, parrayindex)][arrayindex][childname] = Evalue;
}
}
}

/**
* Iterative JSON data Objects set to form fields
*/
function Itervaluefromjsonobject (Jsonobject, ParentName, ChildName) {
Parrayindex is used to determine whether an element is an array label
Parrayindex = Parentname.indexof (' [');
Determines whether the data is aggregated, not just the object property
if (Parrayindex < 0) {
Dotindex = Childname.indexof ('. ');
if (Dotindex > 0) {
Return Itervaluefromjsonobject (Jsonobject[parentname], childname.substring (0, Dotindex), childname.substring ( dotindex+1));
}else{
return Jsonobject[parentname][childname]
}
}else{
Parray = jsonobject[parentname.substring (0, Parrayindex)];
Get the set subscript, and determine if the corresponding subscript exists JS object
arrayindex = parentname.substring (parrayindex+1, parentname.length-1);
var c = jsonobject[parentname.substring (0, Parrayindex)][arrayindex];
Dotindex = Childname.indexof ('. ');
if (Dotindex > 0) {
Return Itervaluefromjsonobject (jsonobject[parentname.substring (0, Parrayindex)][arrayindex], childName.substring ( 0, Dotindex), childname.substring (dotindex+1));
}else{
Return jsonobject[parentname.substring (0, Parrayindex)][arrayindex][childname]
}
}
}

Current 1/3 page 123 Next read the full text
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.