JQuery uses ajax to obtain background json data and assign values to form forms.

Source: Internet
Author: User

JQuery uses ajax to obtain background json data and assign values to form forms.

JQuery provides the load method to load data to the form on the page. However, when I need to obtain a value in the background json data, I need to assign a value to the entire form, the load method does not have a callback function, so it cannot work. If ajax is used to call the load method, obtain the background json data and analyze the json data to assign values to each text box of the form, this is not too complex and too much code, so I summarized a very useful jQuery function based on some answers from the experts.

JQuery-load method call:

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


Page form:

 

Summary:

Page call:

<script>$.getJSON(URL,param,function(data){alert(data.type);$("form").setForm(data);});</script>

Put the following code into jQuery to get

$. 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 the check box and is an Array for (var I = 0; I
 
  
Note that the page starts the ajax method, and then you can get the desired value through ajax, and assign all the values to the form.

After reading it, I feel very loving.







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.