I have:
<select id="isTitles">
<option value="true">enable</option>
<option value="false">disable</option>
</select>
on my index.jsp. And I would like to know what had been chosen:
response.getWriter().write( "User chose: " + request.getAttribute( "isTitles" ) );
But I've got "User chose: null"... = (
The val() method returns the value of selected attribute value. let selectedValue = $("#selectVal option:selected"). val(); The text() method returns a string of the selected option.
Direct value should work just fine: var sel = document. getElementsByName('item'); var sv = sel. value; alert(sv);
You need to pass the name of your attribute as name="isTitles"
<select name="isTitles">
</select>
request.getParameter()
understands the name of the parameter
Change your HTML code to:
<select id="isTitles" name="isTitles" >
The id
attribute of select tag is mainly for DOM usages, and name
attribute is to specify the key of a form data.
And then use request.getParameter("isTitles")
, your will get the right value. getParameter
is for retrieving parameters from form data and URL query string. While getAttribute
is for transfer data through the process chain during the request life cycle.
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