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.
ajaxForm
ajaxForm
in your document's ready
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
.Note: You can pass any of the standard $.ajax
options to ajaxForm
Example:
$('#myFormId').ajaxForm();
ajaxSubmit
ajaxSubmit
takes zero or one argument. The single argument can be either a callback function or an
Options Object
.Note: You can pass any of the standard $.ajax
options to ajaxSubmit
Example:
// 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; });
formSerialize
name1=value1&name2=value2
Example:
var queryString = $('#myFormId').formSerialize(); // the data could now be submitted using $.get, $.post, $.ajax, etc $.post('myscript.php', queryString);
fieldSerialize
name1=value1&name2=value2
Example:
var queryString = $('#myFormId .specialFields').fieldSerialize();
fieldValue
Example:
// get the value of the password input var value = $('#myFormId :password').fieldValue(); alert('The password is: ' + value[0]);
resetForm
Example:
$('#myFormId').resetForm();
clearForm
$('#myFormId').clearForm();
clearFields
$('#myFormId .specialFields').clearFields();
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:
null
action
attribute
method
attribute (or 'GET' if none found)
[ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
Default value: null
Default value: null
dataType
option provides a means for specifying how the server response should be handled. This maps directly to the jQuery.httpData
method. The following values are supported:
'xml': if dataType == 'xml' the server response is treated as XML and the 'success' callback method, if specified, will be passed the responseXML value
'json': if dataType == 'json' the server response will be evaluted and passed to the 'success' callback, if specified
'script': if dataType == 'script' the server response is evaluated in the global context
Default value: null
type="image"
. You should only set the semantic option to true if your server has strict semantic requirements and your form contains an input element of type="image"
.false
null
null
false
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
.