Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected value from drop down list in jsp?

Tags:

jsp

<select name="item">
<c:forEach items="${combo}" var="id">
    <option value="${id}">${id}</option>
</c:forEach>
</select>

How can we get the selected value from the above dropdown list?

like image 435
Pearl Avatar asked Jan 02 '13 11:01

Pearl


2 Answers

I know that this is an old question, but as I was googling it was the first link in a results. So here is the jsp solution:

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

in some.jsp

request.getParameter("item");

this line will return the selected option (from the example it is: 1, 2 or 3)

like image 145
Cԃաԃ Avatar answered Nov 16 '22 08:11

Cԃաԃ


Direct value should work just fine:

var sel = document.getElementsByName('item');
var sv = sel.value;
alert(sv);

The only reason your code might fail is when there is no item selected, then the selectedIndex returns -1 and the code breaks.

like image 45
Milind Anantwar Avatar answered Nov 16 '22 09:11

Milind Anantwar