Recently in thinking about optimizing projects, I want to expand a jquery automatically to get the data in the form for AJAX submissions. I do not have integrity to learn jquery, basically is now learning to find, a little difficult.
The main thing is to expand and splice JSON objects
Very simply, enclose the code:
;
(function ($) {$.fn.ajaxform = function (options) {var defaults = {modelname: ' model ',//Background object receive name URL: '/',//Submit address Posttype: ' POST ',//Submit way DataType: ' JSON ',//Data return type async:false,//whether or not asynchronous option
OBJ: [],//custom parameter Callback:function () {},//successful callback};
var options = $.extend (defaults, options);//merge parameters if (Options.url = = ") {alert (' Please fill in the submission address ');
Return
var postvals = {}; textbox/hidden fields/textarea/radio checked value $ (this). Find (' input[type= "text"],input[type= "hidden"],textarea,input[type= "
Radio "]:checked"). each (function () {if ($ (). Val ()!= undefined) {var name = $ (this). attr (' name ');
if (name = = Undefined | | | name = = ") {return false;
var value = $ (this). Val ();
var json = ' {' + name + ' ': ' + value + '} ';
var obj = $.parsejson (JSON);
Postvals = $.extend (postvals, obj);
}
}); var resobJ
if (options.optionobj!= undefined | | options.optionobj!=[]) {resobj = $.extend (postvals,options.optionobj);
else {resobj = postvals; } $.ajax ({type:options.postType, DataType:options.dataType, Data:resobj, async:opt Ions.async, Url:options.url, Success:function (JSON) {if (JSON). IsError) {alert (JSON).
message);
else {options.callback ();
}
}
});
}; }) (JQuery);
Use words with jquery validate use
$ ("#system-form"). Validate ({
rules: {
systemname: {
required:true
},
Description: {
Required:true,
},
},
messages: {
SystemName: {
required: "Please fill in the system name"
},
Description : {
Required: "Please fill in System description"
}
},
submithandler:function (form) {
var url = '/oa/system/' + $ (form) . attr (' ftype ');
$ (form). Ajaxform ({url:url,modelname: ' System ', Callback:function () {
location.href = '/oa/system/index.html '; c21/>}});
This jquery implementation of the Ajax form information of the simple method (recommended) is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.