Retrieving Form Values

These methods allow scripts to read and transform the values of fields in web forms.

Collects the values in a form into an array of objects.

.formToArray([semantic])

Collects the values in a form into a serialized string.

.formSerialize([semantic])

Collects the values of a set of fields into a serialized string.

.fieldSerialize([successful])

Like the .formSerialize method before it, the .fieldSerialize method fetches the values of a form, and converts them into a string that is appropriate for passing as a query string for a GET request. However, .fieldSerialize() acts on a jQuery object that references individual fields rather than the form as a whole.

It can handle fields of any standard type, such as <select> menus:

The user can then select any option:

Discussion.fieldSerialize()about

The value is pulled from the currently selected option, and the .fieldSerialize method will return a string representation of this value:

Each of the given fields shows up as a key-value pair in the string. Checkbox elements that are not checked do not get represented in the string. The string is URL-encoded as necessary.

By default, fields are not represented in the string if they are not successful, as defined in the W3C specification for HTML forms:

http://www.w3.org/TR/html4/interact/forms.html#h-17.13.2

Successful fields are the ones that are submitted to the server during a normal form submission operation. For example, checkboxes that are currently checked are successful; unchecked ones are not. It is rare to want the values of unsuccessful fields, but if this is required, the successful parameter of .fieldSerialize() can be set to false.

Given the form illustrated in the .ajaxFor() discussion, .fieldSerializer() includes only checked radio buttons and checkboxes when successful is set to true:

tactic=loot&gear%5Bhelmet%5D=yes&gear%5Bgoat%5D=yes

But when successful is set to false, fieldSerializer() includes the unselected options as well:

tactic=loot&tactic=pillage&tactic=burn&gear%5Bhelmet%5D=yes
  &gear%5Blongboat%5D=yes&gear%5Bgoat%5D=yes

Collects the values of a set of fields into an array of strings.

.fieldValue([successful])
$.fieldValue(element[, successful])

The .fieldValue() method and the $.fieldValue() function both fetch the values of a form, returning them as an array of strings. The .fieldValue() method acts on a jQuery object that references individual fields, while the $.fieldValue() function performs the same task on the field element passed as its first parameter.

These operations can handle fields of any standard type, such as <select> menus:

The user can then select any option:

Discussion.fieldValue()about

The value is pulled from the currently selected option, and the .fieldValue() method will return an array representation of this value:

Each of the given fields shows up as a string in the array. Checkbox elements that are not checked do not get represented in the array.

By default, fields are not represented in the array if they are not successful, as defined in the W3C specification for HTML forms:

http://www.w3.org/TR/html4/interact/forms.html#h-17.13.2

Successful fields are the ones that are submitted to the server during a normal form submission operation. For example, checkboxes that are currently checked are successful; unchecked ones are not. It is rare to want the values of unsuccessful fields, but if this is required, the successful parameter of .fieldValue() can be set to false.

Given the form illustrated in the .ajaxFor() discussion, .fieldValue() includes only checked radio buttons and checkboxes when successful is set to to true:

[loot, yes, yes]

But when successful is set to false, .fieldValue() includes the unselected options as well:

[loot, pillage, burn, yes, yes, yes]

The .fieldValue method always returns an array; if there are no values to report in the set of elements being acted upon, the result array will be empty. In contrast, the $.fieldValue function will return null if the field element in question is not successful.