Examples of jquery validator validating Ajax submission forms and methods of Ajax parameters

Source: Internet
Author: User
This paper mainly introduces the method of the jquery validator verifying the AJAX submission form and the method of the Ajax communication, and also mentions jquery in the text. The Ajax submission form, the specific example code for everyone to refer to this article, I hope to help everyone.

The serialize () method creates a URL-encoded text string by serializing the form value. Instead of a way to pass a parameter.

Previously written methods of Ajax communication

$.ajax ({         URL: "${ctx}/sjstandarddampartition/insertorupdatedampartition",         type: "Post",         DataType: " JSON ",         data: {beginsectionid:function () {              return $ (' #number option:selected '). Val ();            },            Beginelevation:function () {              return $ (' #onset '). Val ();            },            endelevation:function () {              return $ (' # End '). Val ();            }}        ,         success:function (Result) {         }       });

The method of using serialize () to transmit parameters

var param = $ ("#standForm"). Serialize ();        $.ajax ({         URL: "${ctx}/sjstandardstandardinfo/insertorupdatestandardinfo",         type: "Post",         DataType: " JSON ",         Data:param,         success:function (Result) {         }       });

When we are in a slightly more complex business, we may encounter the need for multiple form forms on the same page, but when submitting a form does not want the page to refresh or jump, then we consider the AJAX submission form, So how do you get jquery's validator plug-in to validate a form that is being submitted asynchronously? Let's keep looking down.

Here, I'll use an example on the internet to illustrate it.

The following is a more common jquery. How Ajax submits a form

$ ("#submitButton"). Click (function () {//Serialize form   var param = $ ("#leaveSave"). Serialize ();   $.ajax ({    URL: "leavesave.action",    type: "Post",    DataType: "JSON",    Data:param,    success: function (Result) {if (result== ' success ') {location.href= ' allrequisitionlist.action ';} else if (Result.startwith (" Error_ ")) {$ (" #errorMessage "). HTML (result.substring (6));} else {//returns the result converted to JSON data var jsonobj = eval (' (' +result+ ') '); StartTime = $ ("#startdate"). Val (); EndTime = $ ("#enddate"). Val (); hour = Jsonobj.hour; Reason = Jsonobj.reason;  Replacedom (Starttime,endtime,hour,reason); } } }); });

If you want to submit a form with Ajax, and you want to validate it with jquery's validate, you can do this: The form is also a normal form content, type or submit type, but use Ajax to submit the form in the Validate validation pass method

$ ("#saveWorkExtra"). Validate ({onsubmit:true,//whether the commit is validation onfocusout:false,// Whether to verify that onkeyup:false,//is validating the rules when tapping the keyboard when the focus is taken: {...}, messages:{...}, submithandler:function (form) {//After passing callback VA    R param = $ ("#saveToWorkExtra"). Serialize (); $.ajax ({url: "workextrachange.action", type: "Post", DataType: "JSON", Data:param, success:function (Result) {if (re    sult== ' success ') {location.href= ' allrequisitionlist.action ';} else {var jsonobj = eval (' (' +result+ ') ');}}      });      }, Invalidhandler:function (form, validator) {//does not pass callback return false; } });

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.