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()
- $('#myForm').ajaxForm(function() {
- $( '#output1' ).html( "Successful submission! Welcome next time!" ).show();
- });
- $('#myForm2').submit(function() {
- $(this).ajaxSubmit(function() {
- $( '#output2' ).html( "Successful submission! Welcome next time!" ).show();
- });
- return false ; // prevent the form from submitting by default
- });
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.
- var options = {
- target: '#output' , // put the content returned by the server into the element whose id is output
- beforeSubmit: showRequest, //Callback function before submission
- success: showResponse, //callback function after submission
- //url: url, //The default is the action of the form, if it is declared, it will be overwritten
- //type: type, //The default is the form's method (get or post), if it is declared, it will be overwritten
- //dataType: null, //html (default), xml, script, json... accept the type returned by the server
- //clearForm: true, //After successful submission, clear the value of all form elements
- //resetForm: true, //After successful submission, reset the value of all form elements
- timeout: 3000 //limit the time of the request, when the request is greater than 3 seconds, the request will be jumped out
- }
- function showRequest(formData, jqForm, options){
- //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}]
- //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 false is not returned, the form will be submitted, and the form elements can be validated here
- };
- 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 ; // prevent the form from submitting by default
- });
Validate before form submission: beforeSubmit will be called before form submission, if beforeSubmit returns false, it will prevent form submission
- beforeSubmit: validate
- 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
- //Method 1: Use the formData parameter
- for (var i=0; i < formData.length; i++) {
- if (!formData[i].value) {
- alert( 'Username, address and self-introduction cannot be empty!' );
- returnfalse;
- }
- }
- //Method 2: Use the jqForm object
- var form = jqForm[0]; //Convert the form to a dom object
- if (!form.name.value || !form.address.value) {
- alert( 'Username and address cannot be empty, self-introduction can be empty!' );
- returnfalse;
- }
- //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.
- 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!' );
- returnfalse;
- }
- var queryString = $.param(formData); //Assemble data
- //alert(queryString); //Similar: name=1&add=2
- returntrue;
- }