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

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

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. }

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.