Sunny
Sunny

Reputation: 73

Get all select Values in Java

I have a SELECT tag in JSP page that contains few values :

<select name="selectNumber" size="3" multiple="multiple" tabindex="1">
    <option value="11">eleven</option>
    <option value="12">twelve</option>
    <option value="13">thirette</option>
    <option value="14">fourteen</option>
    <option value="15">fifteen</option>
    <option value="16">sixteen</option>
    <option value="17">seventeen</option>
    <option value="18">eighteen</option>
    <option value="19">nineteen</option>
    <option value="20">twenty</option>
  </select>

And I want to get all that values in a Java Class.

What I have tried that :

HttpServletRequest req = (HttpServletRequest)ServletActionContext.getRequest(); String[] selectedNumbers = req.getParameterValues("selectNumber");

But that Array only contains the value I am selecting, and If I don't select any value it contains null. But I want all the option values of the select Tag in java class. Any help?

I am using Struts 2 framework.

Upvotes: 1

Views: 1437

Answers (1)

Neeraj Jain
Neeraj Jain

Reputation: 7730

if you want to send all the Options to Your Servlet then you must first select them all using simply using Jquery

Before Submitting the Form

$("#selectNumber option").prop('selected',true);

Note #is used for Id , which i prefer if you need to use only name then try

$('[name="selectNumber"]');

Upvotes: 2

Related Questions