Form Fields
This page describes and demonstrates the Form Plugin’s fieldValue
and fieldSerialize
methods.
fieldValue
fieldValue
allows you to retrieve the current value of a field. For example, to retrieve the value of the password field in a form with the id of ‘myForm’ you would write:
var pwd = $('#myForm :password').fieldValue()[0];
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.
fieldSerialize
fieldSerialize
allows you to serialize a subset of a form into a query string. This is useful when you need to process only certain fields. For example, to serialize only the text inputs of a form you would write:
var queryString = $('#myForm :text').fieldSerialize();
By default, fieldValue
and fieldSerialize
only function on ‘successful controls’. This means that if you run the following code on a checkbox that is not checked, the result will be an empty array.
// value will be an empty array if checkbox is not checked:
var value = $('#myUncheckedCheckbox').fieldValue();
// value.length == 0
However, if you really want to know the ‘value’ of the checkbox element, even if it’s unchecked, you can write this:
// value will hold the checkbox value even if it's not checked:
var value = $('#myUncheckedCheckbox').fieldValue(false);
// value.length == 1