简体   繁体   English

如何在jsp中获取下拉列表的选定文本

[英]how to get selected text of drop down list in jsp

This is my drop down list in jsp: 这是我在jsp中的下拉列表:

<form action="any.jsp">
  <select name="item">
     <option value="1">Cricket</option>
     <option value="2">Football</option>
     <option value="3">Hockey</option>
  </select>
<input type="submit" value="Submit">
</form>

in any.jsp I want to access the selected text of the drop down list not the value. 在any.jsp中,我想访问下拉列表的选定文本而不是值。 so can anyone please help me with that. 所以任何人都可以帮我。

You can achieve that easily using JQuery in your JSPs , you can give an id to select field. 您可以在JSPs使用JQuery轻松实现这一目标,您可以指定一个ID来选择字段。 And then get the value of selected field using JQUery. 然后使用JQUery获取选定字段的值。

First give an id to select field: 首先提供一个ID以select字段:

<select name="item" id="id1">

then you would get the value of the selected option the following way: 那么您将通过以下方式获得所选选项的值:

var value = $( "#id1 option:selected" ).text();

This value variable will contain the text of the selected field. value变量将包含所选字段的文本。

Or if you don't want to deal with the importing JQuery library etc, you can use simple JavaScript inside your JSP to get the text of selected option like this: 或者,如果您不想处理导入的JQuery库等,则可以在JSP中使用简单的JavaScript来获取所选选项的文本,如下所示:

var abc = document.getElementById("id1");
var value= abc.options[abc.selectedIndex].text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM