Jquery is a simple method for ajax to submit form information (recommended), jqueryajax
Recently I was thinking about optimization projects and wanted to expand a jquery automatic acquisition of data in the form for ajax submission. I am not completely familiar with jquery. Basically, I am learning it now, and it is a little difficult.
It mainly extends and concatenates json objects to convert objects.
It is very easy to attach the Code:
; (Function ($) {$. fn. ajaxForm = function (options) {var defaults = {modelname: 'model', // url of the backend object receiving name: '/', // submit address postType: 'post ', // submission method dataType: 'json', // data return type async: false, // asynchronous optionObj: [], // custom parameter callback: function () {}, // successful callback}; var options = $. extend (defaults, options); // merge the parameter if (options. url = '') {alert ('Enter the submission address'); return;} var postvals ={}; // textbox/hidden domain/textarea/radio selected value $ (This ). find ('input [type = "text"], input [type = "hidden"], textarea, input [type = "radio"]: checked '). each (function () {if ($ (this ). 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: options. async, url: options. url, success: function (json) {if (json. isError) {alert (json. message);} else {options. callback () ;}}) ;}}( jQuery );
Used with jquery validate
$ ("# System-form "). validate ({rules: {SystemName: {required: true}, Description: {required: true, },}, messages: {SystemName: {required: "Enter the system name"}, Description: {required: "enter the 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 ';}});}});
The simple method (recommended) for implementing ajax form submission information in jquery is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the help house.