JQuery form pluginJQuery. form
This is a jQuery plug-in that fully supports forms and supports file upload. It includes the following methods:
-AjaxForm
-AjaxSubmit
-FormToArray
-FormSerialize
-FieldSerialize
-FieldValue
-ClearForm
-ClearFields
-ResetForm
Sample Code:
// Wait for the Dom to be loaded
$ (Document). Ready (function (){
// Bind 'myform' and provide a simple callback function
$ ('# Myform'). ajaxform (function (){
Alert ("Thank you for your comment! ");
});
});
Project home: http://malsup.com/jquery/form/
: Http://malsup.com/jquery/form/jquery.form.js
Form plug-in API
Http://www.malsup.com/jquery/form/#api.
The form plug-in API provides several methods for you to easily manage form data and submit forms.
AjaxForm ()
Add all required event listeners to prepare for AJAX submission forms. The ajaxForm cannot be submitted. In the ready function of document, ajaxForm is used to prepare the AJAX submission form. AjaxForm accepts 0 or 1 parameters. This single parameter can be either a callback function or an Options object.
Chainable: Yes.
Instance:
$ ('# Myformid'). ajaxform ();
AjaxSubmit ()
AJAX will submit the form immediately. In most cases, ajaxSubmit is called to respond to a user's submitted form. AjaxSubmit accepts 0 or 1 parameters. This single parameter can be either a callback function or an Options object.
Chainable: Yes.
Instance:
// Bind the form submission event Processor
$ ('# Myformid'). Submit (function (){
// Submit the form
$ (This). ajaxsubmit ();
// To prevent normal browsers from submitting forms and generating page navigation (to prevent page refreshing ?) Returns false.
Return false;
});
FormSerialize ()
Serializes (or serializes) A form into a query string. This method returns a string in the following format: name1 = value1 & name2 = value2.
Chainable: No. This method returns a string.
Instance:
VaR querystring = $ ('# myformid'). formserialize ();
// You can use $. get, $. post, and $. ajax to submit data.
$. Post ('myscript. php', queryString );
FieldSerialize ()
Serializes (or serializes) The form field elements into a query string. This is convenient when only some form fields need to be serialized (or serialized. This method returns a string in the following format: name1 = value1 & name2 = value2.
Chainable: No. This method returns a string.
Instance:
VaR querystring = $ ('# myformid. specialfields'). fieldserialize ();
FieldValue ()
Returns the value of the form element that matches the inserted array. From version 0.91, this method will always return data in the form of arrays. If the element value is determined to be invalid, the array is empty; otherwise, it contains one or more element values.
Chainable: No. This method returns an array.
Instance:
// Obtain the password input value
VaR value = $ ('# myformid: password'). fieldvalue ();
Alert ('the password is: '+ value [0]);
ResetForm ()
Restore the form to its initial state by calling the original DOM method of the form element.
Chainable: Yes.
Instance:
$ ('# Myformid'). resetform ();
ClearForm ()
Clear form elements. In this method, all text input fields, password input fields, and textarea fields are empty to clear the selected fields in any select element, and reset all the single-choice (radio) buttons and multiple-choice (checkbox) buttons to unselected states.
Chainable: Yes.
$ ('# Myformid'). clearform ();
ClearFields ()
Clear the field element. Only some form elements need to be cleared.
Chainable: Yes.
$ ('# Myformid. specialfields'). clearfields ();
Options object
Both ajaxForm and ajaxSubmit support many option parameters, which can be provided using an Options object. Options is just a JavaScript Object that contains the following set of attributes and values:
Target
Specifies the elements on the page that are updated by the server response. The element value may be specified as a jQuery selector string, A jQuery object, or a DOM element.
Default Value: null.
Url
Specifies the URL for submitting form data.
Default Value: The action attribute value of the form.
Type
Method used to submit form data: "GET" or "POST ".
Default Value: The method property value of the form (if not found, the default value is "GET ").
BeforeSubmit
The callback function called before the form is submitted. The "beforeSubmit" callback function is provided as a hook to run the pre-commit logic or verify the form data. If the "beforeSubmit" callback function returns false, the form will not be submitted. The "beforeSubmit" callback function has three call parameters: form data in array form, jQuery form object, and Options object passed into ajaxForm/ajaxSubmit. The form array accepts the following data:
[{Name: 'username', value: 'jresig'}, {Name: 'Password', value: 'secret'}]
Default Value: null
Success
Callback Function called after the form is successfully submitted. If the "success" callback function is provided, it is called when the slave server returns a response. The value of the dataType option determines whether to return the value of responseText or responseXML.
Default Value: null
DataType
Expected data type. Null, xml, script, or json. DataType provides a method that specifies how to process server responses. This is directly reflected in the jQuery. httpData method. The following values are supported:
'Xml': If dataType = 'xml', the server response is treated as xml. If the "success" Callback method is specified, the responseXML value is returned.
'Json': If ype = 'json', the server response will be evaluated and passed to the "success" Callback method if it is specified.
'Script': If dataType = 'script', the server response value is plain text.
Default Value: null (responseText value returned by the server)
Semantic
Boolean flag indicating whether data must be submitted in strict semantic Order (slower ). note that the normal form serialization is done in semantic order with the exception of input elements of type = "image ". you shoshould only set the semantic option to true if your server has strict semantic requirements and your form contains an input element of type = "image ".
Boolean sign, indicating whether data must strictly follow the semantic Order (slower ?) . Note: In general, the form has been serialized (or serialized) in the semantic order, except for the input element of type = "image. If your server has strict semantic requirements and the form contains an input element of type = "image", set semantic to true. Because this paragraph cannot be understood, the translation may not be satisfactory, but please correct it .)
Default Value: false
ResetForm
Boolean flag, indicating whether to reset the form if it is submitted successfully.
Default Value: NULL
ClearForm
Boolean flag, indicating whether to clear the form data if the form is submitted successfully.
Default Value: NULL
Instance:
// Prepare the options object
VaR Options = {
Target: '# divtoupdate ',
Url: 'comment. php ',
Success: function (){
Alert ('Thanks for your comment! ');
}};
// Pass options to ajaxform
$ ('# Myform'). ajaxform (options );
Note: The Options object can also be used to pass the value to the $. ajax method of jQuery. If you are familiar with the options supported by $. ajax, you can use them to pass Options objects to ajaxForm and ajaxSubmit.
Forwarding: http://blog.csdn.net/spring21st/archive/2008/09/26/2982989.aspx