<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?
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With