JSP下拉列表问题
我想问一个问题
我想在一个JSP页面中显示3个下拉列表,
其中第二个下拉列表基于第一个下拉列表中的选定值,
依此类推...
根据我的有限据了解,当用户从下拉列表中选择一个值时,
用户需要单击一个按钮将该值传递到服务器端。
像这样:
<form action="second_drop.jsp">
<select name="drop">
...
</select>
<input type="submit" value="Submit" />
</form>
现在,我想问一个问题,
是否可以在不单击按钮的情况下传递下拉列表值?
也就是说,当用户从下拉列表中选择一个值时,
所选值可以自动传递到服务器端,而无需单击提交按钮。
然后服务器将根据所选值显示第二个下拉列表,
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 ajax 的神秘力量绝对可以做到这一点!您正在寻找的这种特殊行为称为“链接下拉菜单”或“依赖下拉菜单”。以下是我使用 jQuery 的方法,大量评论,因此(希望)很清楚:
标记
JavaScript
second_drop。 jsp
应该以列表进行响应:
如果您不熟悉 JavaScript 和/或 jQuery,这里有很多内容,所以只需询问是否有您不熟悉的内容不明白。
使用 jQuery 函数
$()
,简写为$(document).ready()
$()
,选择器函数.change()
.val()
.load()
.attr()
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()
这是可能的,有时与地址下拉列表一起使用,例如选择一个国家,然后填充州/省的列表,也与航班一起使用,例如选择出发机场,然后过滤可能的目的地列表。
为此,您需要客户端 Javascript。您需要连接 onchange 事件,以便它提交表单(或向另一个页面发出请求)以与服务器进行通信以获取新列表。
例如(尽管您可能需要设置一个隐藏字段来表示“这是下拉刷新,而不是“我已完成输入数据”提交)
您可能想阅读 JSP:下拉列表 2 取决于下拉列表 1 - 那里有多种方法可以做到这一点,这取决于您需要做什么。
This is possible and is sometimes used with address dropdowns, e.g. select a country and then the list of states/provinces is populated, and also with flights, e.g. select a departure airport, and the list of possible destinations is filtered.
To do this you will need client-side Javascript. You will need to wire up the onchange event so that it will submit the form (or make a request to another page) to communicate back with the server to get the new list.
e.g. (although you will need to set maybe a hidden field to say "this is a dropdown refresh, not an "I'm finished entering my data" submit)
You might want to read the answers to JSP: drop down list 2 depends on drop down list 1 - there are multiple ways to do it and it depends on what you need to do.