Form Plugin API
http://malsup.com/jquery/form/#getting-started
The Form Plugin API provides several methods that allow you to easily manage form data and form submission.
-
Prepares a form to be submitted via AJAX by adding all of the necessary event listeners. It does not submit the form. Use
ajaxForm
in your document'sready
function to prepare your form(s) for AJAX submission.ajaxForm
takes zero or one argument. The single argument can be either a callback function or an Options Object .
Chainable: Yes.Note: You can pass any of the standard
$.ajax
options to ajaxFormExample:
$('#myFormId').ajaxForm();
-
Immediately submits the form via AJAX. In the most common use case this is invoked in response to the user submitting the form.
ajaxSubmit
takes zero or one argument. The single argument can be either a callback function or an Options Object .
Chainable: Yes.Note: You can pass any of the standard
$.ajax
options to ajaxSubmitExample:
// attach handler to form's submit event $('#myFormId').submit(function() { // submit the form $(this).ajaxSubmit(); // return false to prevent normal browser submit and page navigation return false; });
-
Serializes the form into a query string. This method will return a string in the format:
name1=value1&name2=value2
Chainable: No, this method returns a String.Example:
var queryString = $('#myFormId').formSerialize(); // the data could now be submitted using $.get, $.post, $.ajax, etc $.post('myscript.php', queryString);
-
Serializes field elements into a query string. This is handy when you need to serialize only part of a form. This method will return a string in the format:
name1=value1&name2=value2
Chainable: No, this method returns a String.Example:
var queryString = $('#myFormId .specialFields').fieldSerialize();
-
Returns the value(s) of the element(s) in the matched set in an array. As of version .91, this method always returns an array. If no valid value can be determined the array will be empty, otherwise it will contain one or more values.
Chainable: No, this method returns an array.Example:
// get the value of the password input var value = $('#myFormId :password').fieldValue(); alert('The password is: ' + value[0]);
-
Resets the form to its original state by invoking the form element's native DOM method.
Chainable: Yes.Example:
$('#myFormId').resetForm();
-
Clears the form elements. This method emptys all of the text inputs, password inputs and textarea elements, clears the selection in any select elements, and unchecks all radio and checkbox inputs.
Chainable: Yes.$('#myFormId').clearForm();
-
Clears field elements. This is handy when you need to clear only a part of the form.
Chainable: Yes.$('#myFormId .specialFields').clearFields();
ajaxForm
ajaxSubmit
formSerialize
fieldSerialize
fieldValue
resetForm
clearForm
clearFields
The Options Object
Note: You can pass any of the standard $.ajax
options to ajaxForma and ajaxSubmit.
Both ajaxForm
and ajaxSubmit
support numerous options which can be provided using an Options Object. The Options Object is simply a JavaScript Object that contains properties with values set as follows:
Example:
// prepare Options Object
var options = {
target: '#divToUpdate',
url: 'comment.php',
success: function() {
alert('Thanks for your comment!');
}
};
// pass options to ajaxForm
$('#myForm').ajaxForm(options);
Note that the Options Object can also be used to pass values to jQuery's $.ajax
method. If you are familiar with the options supported by $.ajax
you may use them in the Options Object passed to ajaxForm
and ajaxSubmit
.