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.