jQuery in Action
Making GET and POST requests
241
function(data){
$(dropdownSet).loadSelect(data);
adjustSizeDropdown();
}
);
}
}
After obtaining the value of the style dropdown and tucking it away in variable
styleValue
for later reference, we form a wrapped set consisting of the color
dropdown and store it in variable
dropdownSet
. We're going to reference this set
repeatedly throughout the remainder of the function, and we don't want to incur
the overhead of re-creating it every time we need it.
the overhead of re-creating it every time we need it.
The decision whether the color dropdown should be enabled or disabled is
made next
b
, depending on the value of the style dropdown--disabled if the
value is empty, enabled if otherwise. If the color dropdown is disabled, it's also
emptied through use of the
emptied through use of the
emptySelect()
command
c
.
Wait a minute! What
emptySelect()
command?
Before you start feverishly thumbing through the previous chapters searching
for this command, don't bother; it doesn't exist--at least not yet. This command
is one that we'll create ourselves in the next section. For now, be aware that this
command will cause all the options in the color dropdown to be removed.
is one that we'll create ourselves in the next section. For now, be aware that this
command will cause all the options in the color dropdown to be removed.
After we disable and empty the color dropdown, we call the
adjustSizeDrop-
down()
function
c
to make sure that any appropriate adjustments are made to its
dependent size dropdown.
If the color dropdown is enabled, it needs to be filled with the values that are
appropriate for the value that is selected from the styles dropdown. To obtain
those values, we use the services of the
those values, we use the services of the
$.getJSON()
function,
d
specifying a
server-side resource of
getColors.jsp
and passing it the selected style value via
a request parameter named
style
.
When the callback function is invoked (after the Ajax request returns its
response), the parameter passed to the callback is the JavaScript value resulting
from the evaluation of the response as a
from the evaluation of the response as a
JSON
construct. A typical
JSON
construct
returned from
getColors.jsp
is as follows:
[
{value:'',caption:'choose color'},
{value:'bk',caption:'Black Oil-tanned'},
{value:'br',caption:'Black Polishable'}
]
Triggers adjustment of
dependent dropdown
dependent dropdown
e