jQuery plugin -- Form form plugin jquery.form.js

The jQuery Form plugin is an excellent Ajax form plugin that can easily and non-invasively upgrade HTML forms to support Ajax. jQuery Form has two core methods - ajaxForm() and ajaxSubmit(), which aggregate functionality from controlling form elements to deciding how to manage the submission process. In addition, the plugin also includes some other methods: formToArray(), formSerialize(), fieldSerialize(), fieldValue(), clearForm(), clearFields() and resetForm(), etc.

Download address: http://malsup.com/jquery/form/#download

Core methods -- ajaxForm() and ajaxSubmit()

[javascript] view plain copy
print ?
  1. $('#myForm').ajaxForm(function() {     
  2.    $( '#output1' ).html( "Successful submission! Welcome next time!" ).show();      
  3. });    
  4.          
  5. $('#myForm2').submit(function() {  
  6.    $(this).ajaxSubmit(function() {     
  7.       $( '#output2' ).html( "Successful submission! Welcome next time!" ).show();      
  8.    });  
  9.    return false // prevent the form from submitting by default   
  10. });  
Through the two core methods of the Form plugin, you can easily upgrade the form submission method to Ajax submission method without modifying the HTML code structure of the form. Both
ajaxForm() and ajaxSubmit() can accept 0 or 1 Parameter, when it is a single parameter, the parameter can be either a callback function or an options object. The above example is a callback function. The options object is introduced below so that they have more control over the form.

[javascript] view plain copy
print ?
  1. var  options = {  
  2.    target:  '#output' ,           // put the content returned by the server into the element whose id is output      
  3.    beforeSubmit: showRequest,   //Callback function before submission  
  4.    success: showResponse,       //callback function after submission  
  5.    //url: url, //The default is the action of the form, if it is declared, it will be overwritten  
  6.    //type: type, //The default is the form's method (get or post), if it is declared, it will be overwritten  
  7.    //dataType: null, //html (default), xml, script, json... accept the type returned by the server  
  8.    //clearForm: true, //After successful submission, clear the value of all form elements  
  9.    //resetForm: true, //After successful submission, reset the value of all form elements  
  10.    timeout: 3000                //limit the time of the request, when the request is greater than 3 seconds, the request will be jumped out  
  11. }  
  12.   
  13. function showRequest(formData, jqForm, options){  
  14.    //formData: Array object, when submitting the form, the Form plugin will automatically submit the data in Ajax mode, the format is: [{name:user,value:val},{name:pwd,value:pwd}]  
  15.    //jqForm: jQuery object, which encapsulates the elements of the form     
  16.    //options: options object  
  17.    var queryString = $.param(formData);   //name=1&address=2  
  18.    var  formElement = jqForm[0];               //Convert jqForm to DOM object  
  19.    var  address = formElement.address.value;   //Access the DOM element of jqForm  
  20.    return true ;   //As long as false is not returned, the form will be submitted, and the form elements can be validated here   
  21. };  
  22.   
  23. function showResponse(responseText, statusText){  
  24.    //dataType=xml  
  25.    var name = $('name', responseXML).text();  
  26.    var address = $('address', responseXML).text();  
  27.    $("#xmlout").html(name + "  " + address);  
  28.    //dataType=json  
  29.    $("#jsonout").html(data.name + "  " + data.address);  
  30. };  
  31.   
  32. $("#myForm").ajaxForm(options);  
  33.   
  34. $("#myForm2").submit(funtion(){  
  35.    $(this).ajaxSubmit(options);  
  36.    return false ;    // prevent the form from submitting by default   
  37. });  

Validate before form submission: beforeSubmit will be called before form submission, if beforeSubmit returns false, it will prevent form submission

[javascript] view plain copy
print ?
  1. beforeSubmit: validate  
  2. function  validate(formData, jqForm, options) {  //Validate the form here, if it does not meet the rules, it will return false to prevent the form from submitting until it meets the rules  
  3.    //Method 1: Use the formData parameter  
  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.             returnfalse;   
  8.         }  
  9.     }   
  10.   
  11.    //Method 2: Use the jqForm object  
  12.    var  form = jqForm[0];  //Convert the form to a dom object  
  13.       if (!form.name.value || !form.address.value) {  
  14.             alert( 'Username and address cannot be empty, self-introduction can be empty!' );  
  15.             returnfalse;   
  16.       }  
  17.   
  18.    //Method 3: Using the fieldValue() method, fieldValue is a method of the form plugin, which can find the value of the element in the form and return a collection.  
  19.    var usernameValue = $('input[name=name]').fieldValue();  
  20.    var addressValue = $('input[name=address]').fieldValue();  
  21.    if (!usernameValue[0] || !addressValue[0]) {  
  22.       alert( 'Username and address cannot be empty, self-introduction can be empty!' );  
  23.       returnfalse;   
  24.    }  
  25.   
  26.     var  queryString = $.param(formData);  //Assemble data  
  27.     //alert(queryString); //Similar: name=1&add=2    
  28.     returntrue;   
  29. }  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324937485&siteId=291194637