Jquery is a simple method for ajax to submit form information (recommended), jqueryajax

Source: Internet
Author: User

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.

Related Article

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.