jquery obtains background JSON data through Ajax to assign values to form forms

Source: Internet
Author: User

jquery provides the Load method to load the data into the page form form, but sometimes I need to get a value in the background JSON data, but also need to assign the entire form form, the Load method does not have a callback function, so it is not, if the use of Ajax calls, After obtaining the JSON data in the background to analyze the JSON data in a single assignment to form the form of each text box, this is too complicated and too much code, so I based on a number of great God's answer, summed up a very useful jquery function.

Jquery-load Method Invocation:

$ (' #form '). Form (' Load ',URL);


Page form:

<span style= "FONT-SIZE:18PX;" ><form id= "form" action= "system/info/area_operate.html" method= "POST" class= "Default" >    <input type= "Text" name= "Area_name" readonly/>       <input type= "text" name= "name"/><select id= "type" name= "type" > <option value= "1" > Store </option><option value= "2" > HQ </option></select>           < TextArea class= "Easyui-validatebox" name= "remark" cols= "+" rows= "5" required= "false" ></textarea></form ></span>

Summary of the method:

Page Call:

<span style= "FONT-SIZE:18PX;" ><script><span style= "White-space:pre" ></span>$.getjson (url,param,function (data) {<span Style= "White-space:pre" ></span>alert (data.type); <span style= "White-space:pre" ></span>$ (" Form "). Setform (data);}); </script></span>

Put this piece of code into jquery and take

<span style= "font-size:18px;"    >$.fn.setform = function (jsonvalue) {alert ("Setform");    var obj=this;     $.each (Jsonvalue, function (name, ival) {var $oinput = Obj.find ("input:[name=" + name + "]"); if ($oinput. attr ("type") = = "Radio" | | $oinput. attr ("type") = = "checkbox") {$oinput. each (function () {if ( Object.prototype.toString.apply (ival) = = ' [object Array] ') {//Is a check box and is an array for (Var i=0;i<ival.length;                      i++) {if ($ (this). Val () ==ival[i]) $ (this). attr ("Checked", "checked"); }}else{if ($ (this). Val () ==ival) $ (this). attr ("Checke                 D "," checked ");    }             });    }else if ($oinput. attr ("type") = = "textarea") {//Multiline text box Obj.find ("[Name=" +name+ "]"). HTML (ival);         }else{Obj.find ("[Name=" +name+ "]"). Val (ival); }   });}; </span> 

Note that the page launches the Ajax method, then you can get the value you want through AJAX, and you can assign the value to the form form.

After watching the wood has a feeling of love ah.







jquery obtains background JSON data through Ajax to assign values to form forms

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.