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; } });