I would like to ask a question
I want to show 3 drop down list in one JSP page,
where the the second drop down list is based on the selected value in the 1st drop down list,
and so on…
According to my limited knowledge, when user selects a value from drop down list,
the user needs to click a button to pass the value to server side.
like this:
<form action="second_drop.jsp">
<select name="drop">
...
</select>
<input type="submit" value="Submit" />
</form>
Now, I would like to ask a question,
is it possible to pass the drop down list value without clicking button?
that is, when users select a value from drop down list,
the selected value can be automatically passed to server side without clicking submit button.
then the server will based on the selected value to display the second drop down list
thanks for help.
Best Answer
It is absolutely possible to do this using the mystical power of ajax! This particular behavior you're looking for is called "chained dropdowns" or "dependent dropdowns." Here's how I'd do it with jQuery, heavily commented so it's (hopefully) clear:
Markup
JavaScript
second_drop.jsp
should respond with a list of<option>
s:There's a lot there, if you're not familiar with JavaScript and/or jQuery, so just ask if there's anything you don't understand.
jQuery functions used
$()
, shorthand for$(document).ready()
$()
, the selector function.change()
.val()
.load()
.attr()