The JQuery form plugin is an excellent AJAX form plug-in that allows you to upgrade HTML forms easily and without intrusion to support Ajax. The JQuery form has two core methods-Ajaxform () and Ajaxsubmit (), which gather functionality from controlling form elements to deciding how to manage the commit process. In addition, plugins include other methods: Formtoarray (), Formserialize (), Fieldserialize (), Fieldvalue (), ClearForm (), Clearfields (), and Resetform () and so on.
: http://malsup.com/jquery/form/#download
Core methods--ajaxform () and Ajaxsubmit ()
[JavaScript]View PlainCopyPrint?
- $ (' #myForm '). Ajaxform (function() {
- $ (' #output1 '). html ("submit successfully! Welcome to come again next time! "). Show ();
- });
- $ (' #myForm2 '). Submit (function() {
- $ (this). Ajaxsubmit (function() {
- $ (' #output2 '). html ("submit successfully! Welcome to come again next time! "). Show ();
- });
- return false; //Block form default submission
- });
$ (' #myForm '). Ajaxform (function () { $ (' #output1 '). HTML ("submitted successfully! Welcome to come again next time! "). Show (); }); $ (' #myForm2 '). Submit (function () { $ (this). Ajaxsubmit (function () { $ (' #output2 '). HTML ("Submit succeeded!"). Welcome to come again next time! "). Show (); }); return false; Block form default submission});
With the two core methods of the form plug-in, you can easily escalate the form's submission to an AJAX submission without modifying the form's HTML code structure.
Both Ajaxform () and Ajaxsubmit () can accept 0 or 1 parameters, which can be either a callback function or an options object when a single parameter is used, and the example above is the callback function, which describes the options object, So that they have more control over the form
[JavaScript]View PlainCopyPrint?
- var options = {
- Target: ' #output ', //Put the contents returned by the server into an element with ID output
- Beforesubmit:showrequest, //Pre-commit callback function
- Success:showresponse, //post-commit callback function
- //url:url,//default is the action of form, if stated, Overrides
- //type:type,//default is form method (get or post), if stated, Overrides
- //datatype:null,//html (default), XML, script, JSON ... Accept the type returned by the server
- //clearform:true,//After successful commit, clears the values of all form elements
- //resetform:true,//After successful commit, resets the values of all form elements
- timeout:3000 //Limit the request time, when the request is greater than 3 seconds, jump out of the request
- }
- function showrequest (FormData, Jqform, options) {
- //formdata: An Array object, when the form is submitted, the form plug-in automatically submits the data in an AJAX format such as: [{name:user,value:val},{name:pwd,value:pwd}]
- //jqform:jquery object that encapsulates the elements of the form
- //options:options Object
- var queryString = $.param (FormData); //name=1&address=2
- var formelement = jqform[0]; //Convert Jqform to DOM object
- var address = FormElement.address.value; //Access DOM elements of Jqform
- return true; //The form will be submitted, as long as it does not return false, where the form elements can be validated
- };
- function showresponse (ResponseText, statustext) {
- //datatype=xml
- var name = $ (' name ', responsexml). text ();
- var address = $ (' address ', responsexml). text ();
- $ ("#xmlout"). HTML (name + "" + address);
- //datatype=json
- $ ("#jsonout"). html (data.name + "" + data.address);
- };
- $ ("#myForm"). Ajaxform (options);
- $ ("#myForm2"). Submit (Funtion () {
- $ (this). Ajaxsubmit (options);
- return false; //block form default submission
- });
var options = {target: ' #output ',//Put the contents of the server back into an element with ID output beforesubmit:showrequest,//Pre-commit callback function Su Ccess:showresponse,//post-commit callback function//url:url,//default is the action of form, if declared, will overwrite//type:type, The default is form's method (get or post), if stated, overwrites//datatype:null,//html (default), XML, script, JSON ... Accepts the type//clearform:true returned by the server,//After successful commit, clears the value of all form elements//resetform:true,//After successful commit, resets the values of all form elements timeout:30 00//Limit the request time, when the request is greater than 3 seconds, jump out of the request}function Showrequest (FormData, Jqform, Options) {//formdata: Array object, when the form is submitted, the form plug-in will The data is automatically submitted in AJAX format, such as: [{name:user,value:val},{name:pwd,value:pwd}]//jqform:jquery object, which encapsulates the elements of the form//options:options object var queryString = $.param (FormData); name=1&address=2 var formelement = jqform[0]; Convert Jqform to DOM object var address = FormElement.address.value; Access the DOM element of Jqform return true; As long as you do not return false, the form will be submitted, where the form elements can be verified};function showresponse (ResponseText, StatustEXT) {//datatype=xml var name = $ (' name ', responsexml). text (); var address = $ (' address ', responsexml). text (); $ ("#xmlout"). HTML (name + "" + address); Datatype=json $ ("#jsonout"). HTML (data.name + "" + data.address);}; $ ("#myForm"). Ajaxform (Options), $ ("#myForm2"). Submit (Funtion () {$ (this). Ajaxsubmit (options); return false; Block form default submission});
Validation before form submission: Beforesubmit is called before the form is submitted, and if Beforesubmit returns false, it prevents the form from being submitted
[JavaScript]View PlainCopyPrint?
- Beforesubmit:validate
- function Validate (FormData, Jqform, options) { //To validate the form here, and false to prevent the form from submitting until the rule is met if the rule is not met
- //Mode one: using Formdata parameters
- for (var i=0; i < formdata.length; i++) {
- if (!formdata[i].value) {
- Alert (' username, address and self-introduction cannot be empty! ' ) );
- return false;
- }
- }
- //Mode two: Using Jqform object
- var form = jqform[0]; //Convert form to DOM object
- if (!form.name.value | |!form.address.value) {
- Alert (' username and address cannot be empty, self-introduction can be empty! ');
- return false;
- }
- //Way three: Using the Fieldvalue () method, Fieldvalue is a form plug-in method that can find the values of elements in a form and return a collection.
- var Usernamevalue = $ (' input[name=name] '). Fieldvalue ();
- var Addressvalue = $ (' input[name=address] '). Fieldvalue ();
- if (!usernamevalue[0] | |!addressvalue[0]) {
- Alert (' username and address cannot be empty, self-introduction can be empty! ');
- return false;
- }
- var queryString = $.param (formData); //Assembly data
- //alert (queryString);//similar: name=1&add=2
- return true;
- }
Beforesubmit:validatefunction Validate (FormData, Jqform, Options) {//To validate the form here, and false to prevent the form from submitting until the rule is met if the rule is not met //Mode one: Use the FormData parameter for (var i=0; i < formdata.length; i++) { if (!formdata[i].value) { alert (' User name, Address and self-introduction are not empty! '); return false; } } Mode two: Use Jqform object var form = jqform[0];//Convert form to DOM object if (!form.name.value | |!form.address.value) { alert (' User name and address cannot be empty, self-introduction can be empty! '); return false; } Method Three: Using the Fieldvalue () method, Fieldvalue is a form plug-in method that can find the values of the elements in the form, returning a collection. var usernamevalue = $ (' input[name=name] '). Fieldvalue (); var addressvalue = $ (' input[name=address] '). Fieldvalue (); if (!usernamevalue[0] | |!addressvalue[0]) { alert (' username and address cannot be empty, self-introduction can be empty! '); return false; } var queryString = $.param (FormData); Assembly Data //alert (queryString);//similar to: name=1&add=2 return true;}
JQuery plugin-form form plug-in jquery.form.js