jquery Plugin-Form form plug-in jquery.form.js

Source: Internet
Author: User

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?
  1. $ (' #myForm '). Ajaxform (function() {
  2. $ (' #output1 '). html ("submit successfully! Welcome to come again next time!      "). Show ();
  3. });
  4. $ (' #myForm2 '). Submit (function() {
  5. $ (this). Ajaxsubmit (function() {
  6. $ (' #output2 '). html ("submit successfully! Welcome to come again next time!      "). Show ();
  7. });
  8. return false; //Block form default submission   
  9. });
$ (' #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?
  1. var options = {
  2. Target: ' #output ', //Put the contents returned by the server into an element with ID output
  3. Beforesubmit:showrequest, //Pre-commit callback function
  4. Success:showresponse, //post-commit callback function
  5. //url:url,//default is the action of form, if stated, Overrides   
  6. //type:type,//default is form method (get or post), if stated, Overrides   
  7. //datatype:null,//html (default), XML, script, JSON ... Accept the type returned by the server
  8. //clearform:true,//After successful commit, clears the values of all form elements   
  9. //resetform:true,//After successful commit, resets the values of all form elements   
  10. timeout:3000 //Limit the request time, when the request is greater than 3 seconds, jump out of the request
  11. }
  12. function showrequest (FormData, Jqform, options) {
  13. //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}]   
  14. //jqform:jquery object that encapsulates the elements of the form   
  15. //options:options Object   
  16. var   queryString = $.param (FormData); //name=1&address=2
  17. var              formelement = jqform[0]; //Convert Jqform to DOM object
  18. var  address = FormElement.address.value; //Access DOM elements of Jqform
  19. return   true; //The form will be submitted, as long as it does not return false, where the form elements can be validated
  20. };
  21. function showresponse (ResponseText, statustext) {
  22. //datatype=xml   
  23. var  name = $ (' name ', responsexml). text ();
  24. var  address = $ (' address ', responsexml). text ();
  25. $ ("#xmlout"). HTML (name + "" + address);
  26. //datatype=json   
  27. $ ("#jsonout"). html (data.name + "" + data.address);
  28. };
  29. $ ("#myForm"). Ajaxform (options);
  30. $ ("#myForm2"). Submit (Funtion () {
  31. $ (this). Ajaxsubmit (options);
  32. return    false; //block form default submission
  33. });
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?
  1. Beforesubmit:validate
  2. 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
  3. //Mode one: using Formdata parameters   
  4. for (var i=0; i < formdata.length; i++) {
  5. if (!formdata[i].value) {
  6. Alert (' username, address and self-introduction cannot be empty! ' )  );
  7. return   false;
  8. }
  9. }
  10. //Mode two: Using Jqform object   
  11. var form = jqform[0]; //Convert form to DOM object   
  12. if (!form.name.value | |!form.address.value) {
  13. Alert (' username and address cannot be empty, self-introduction can be empty!  ');
  14. return   false;
  15. }
  16. //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.   
  17. var  Usernamevalue = $ (' input[name=name] '). Fieldvalue ();
  18. var  Addressvalue = $ (' input[name=address] '). Fieldvalue ();
  19. if (!usernamevalue[0] | |!addressvalue[0]) {
  20. Alert (' username and address cannot be empty, self-introduction can be empty!  ');
  21. return   false;
  22. }
  23. var queryString = $.param (formData); //Assembly data   
  24. //alert (queryString);//similar: name=1&add=2   
  25. return   true;
  26. }
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

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.