如何从下拉列表中获取选定的选项标签?

发布于 2024-12-26 16:29:12 字数 193 浏览 1 评论 0原文

我正在开发一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2025-01-02 16:29:12

您需要在服务器端维护选项值和标签的映射。例如,在某些 ServletContextListener 或 servlet 的 init() 中:

Map<String, String> countries = new LinkedHashMap<String, String>();
countries.put("CW", "Curaçao");
countries.put("NL", "The Netherlands");
countries.put("US", "United States");
// ...

servletContext.setAttribute("countries", countries);

当您将其作为 ${countries} 放在应用程序范围中时,您可以显示如下:

<select name="country">
  <c:forEach items="${countries}" var="country">
    <option value="${country.key}">${country.value}</option>
  </c:forEach>
</select>

这样您就可以在服务器端获取标签,如下所示:

Map<String, String> countries = (Map<String, String>) getServletContext().getAttribute("countries");
// ...

String countryCode = request.getParameter("country");
String countryName = countries.get(countryCode);
// ...

或在 JSP 中显示纯文本:

<p>Country code: ${param.country}</p>
<p>Country name: ${countries[param.country]}</p>

或预先选择下拉列表:

<select name="country">
  <c:forEach items="${countries}" var="country">
    <option value="${country.key}" ${param.country == country.key ? 'selected' : ''}>${country.value}</option>
  </c:forEach>
</select>

You need to maintain a mapping of option values and labels in the server side. E.g. inside some ServletContextListener or perhaps servlet's init():

Map<String, String> countries = new LinkedHashMap<String, String>();
countries.put("CW", "Curaçao");
countries.put("NL", "The Netherlands");
countries.put("US", "United States");
// ...

servletContext.setAttribute("countries", countries);

When you put it in the application scope as ${countries}, then you can display it as follows:

<select name="country">
  <c:forEach items="${countries}" var="country">
    <option value="${country.key}">${country.value}</option>
  </c:forEach>
</select>

This way you will be able to obtain the label in the server side as follows:

Map<String, String> countries = (Map<String, String>) getServletContext().getAttribute("countries");
// ...

String countryCode = request.getParameter("country");
String countryName = countries.get(countryCode);
// ...

Or to display plain in JSP:

<p>Country code: ${param.country}</p>
<p>Country name: ${countries[param.country]}</p>

Or to pre-select the dropdown:

<select name="country">
  <c:forEach items="${countries}" var="country">
    <option value="${country.key}" ${param.country == country.key ? 'selected' : ''}>${country.value}</option>
  </c:forEach>
</select>
心清如水 2025-01-02 16:29:12

无需在服务器端存储任何内容即可完成此操作。

<select name="menu" id="menu">
<option value="1">label 1</option>
<option value="2">label 2</option>
</select>

<button onclick='show()'>Click me</button>

<script type="text/javascript">
function show(){
var theContents = document.getElementById('menu')[document.getElementById('menu').selectedIndex].innerText;
window.alert(theContents);
}
</script>

This can be done without storing anything on server side.

<select name="menu" id="menu">
<option value="1">label 1</option>
<option value="2">label 2</option>
</select>

<button onclick='show()'>Click me</button>

<script type="text/javascript">
function show(){
var theContents = document.getElementById('menu')[document.getElementById('menu').selectedIndex].innerText;
window.alert(theContents);
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文