jQuery in Action

270
CHAPTER 9
Prominent, powerful, and practical plugins
It augments the form functionalities in three areas:
Getting the values of form controls
Clearing and resetting form controls
Submitting forms (including file uploads) via Ajax
Let's start with getting form control values.
9.1.1 Getting form control values
The Form Plugin gives us two ways to get the values of form controls: as an
array of values or as a serialized string. There are three methods that the Form
Plugin provides to obtain control values:
fieldValue()
,
formSerialize()
, and
fieldSerialize()
.
Let's look at grabbing field values first.
Getting control values
We can get the values of form controls using the
fieldValue()
command. At first
glance, you might think that
fieldValue()
and
val()
are redundant. But prior to
jQuery 1.2, the
val()
command was considerably less capable, and
fieldValue()
was designed to make up for its deficiencies.
The first major difference is that
fieldValue()
returns an array of all the val-
ues for form controls in its wrapped set, whereas
val()
only returns the value of
the first element (and only if that element is a form control). In fact,
fieldValue()
always returns an array, even if it only has one value or no values to return.
Another difference is that
fieldValue()
ignores any non-control elements in
the wrapped set. If we create a set containing all elements on a page, an array
that contains only as many control values as
fieldValue()
finds will be
returned. But not all controls have values in this returned array: like the
val()
command,
fieldValue()
, by default, returns values only for controls that are
deemed successful.
So what's a successful control? It's not a control that can afford a stable of fancy
sports cars, but a formal definition in the
HTML
Specification
1
that determines
whether a control's value is significant or not and whether it should be submitted
as part of the form.
We won't go into exhaustive detail here; but, in a nutshell, successful controls
are those that have
name
attributes, aren't disabled, and are checked (for check-
able controls like check boxes and radio buttons). Some controls, like reset and
1
http://www.w3.org/TR/REC-html40/


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.