jquery implements a simple method of submitting form information with Ajax (recommended) _jquery

Source: Internet
Author: User

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.

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.