简体   繁体   中英

how to get selected text of drop down list in jsp

This is my drop down list in 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. 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. And then get the value of selected field using JQUery.

First give an id to select field:

<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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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