如何从下拉列表中获取选定的选项标签?
我正在开发一个简单的 Web 应用程序,我想在下一个 JSP 页面上的 HTML 页面中获取下拉列表的选项标签。我正在使用 MVC 模式,因此 Servlet 作为控制器会将请求重定向(转发?)到 JSP 视图。
request.getParameter()
只给我选项值。但就我而言,选项值和标签是不同的。我如何获得选项标签?
I am developing a simple web application in which I want to take the option label of a dropdown list in HTML page on the next JSP page. I am using MVC pattern and thus Servlet as a controller will be redirecting (forwarding?) the request to JSP view.
The request.getParameter()
gives me only the option value. But in my case the option value and label are different. How can I get the option label?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在服务器端维护选项值和标签的映射。例如,在某些
ServletContextListener
或 servlet 的init()
中:当您将其作为
${countries}
放在应用程序范围中时,您可以显示如下:这样您就可以在服务器端获取标签,如下所示:
或在 JSP 中显示纯文本:
或预先选择下拉列表:
You need to maintain a mapping of option values and labels in the server side. E.g. inside some
ServletContextListener
or perhaps servlet'sinit()
:When you put it in the application scope as
${countries}
, then you can display it as follows:This way you will be able to obtain the label in the server side as follows:
Or to display plain in JSP:
Or to pre-select the dropdown:
无需在服务器端存储任何内容即可完成此操作。
This can be done without storing anything on server side.