简体   繁体   中英

How to get all values from a select box(selected & not selected) in jsp

I have a HTML select box which have multi select enabled in my jsp. I'm populating options for that dynamically from other button. So user won't be selecting any values from that select box. I have to pass all the values of my selectbox to other jsp.

I'm navigating through jsp using form action submit. When doing that I cannot able to get all my select box values.

request.getParameterValues

This one giving only the selected values from that multi-select box. What I want is to get all values from that selectbox no matter selected or not.

Thanks;

Well simply loop through your select and get every option value, try the following:

var select = document.getElementById('mySelect');
for(var i=0; i<select.options.length;i++){
    console.log(select.options[i].value);
}

This is a DEMO Fiddle .

For this you can use Web Storage for store the value and getting back that value form Web Storage .

For more detail you can visit below links.

Code Below

  • you can store data in 1st Page (Html1.html) as below code :

localStorage.setItem("key", "value");

  • you can get the data on 2nd Page (Html2.html) as below code :

var valueFromFirstPage = localStorage.getItem("key");

You can store the list of values to the HttpSession and retrieve them wherever you need for the user session .

In your jsp ,

 HttpSession session =request.getSession(false);
 session.setAttribute("ListName", yourListHere);

to set the values to the session and to retrieve them,

session.getAttribute("ListName");

Related:

In your first jsp, have a hidden field with all possible values for the select box, so that on the next jsp you will have the values by the given name, of-course name will be different for hidden field and the select box.

CSV hidden

first.jsp

<select name='chosen' multiple>
  ...
</select>
<input type='hidden' name='toChoose' value='1,2,3,4'/>

next.jsp

String[] chosen = request.getParameterValues("chosen");
String[] toChoose = request.getParameter("toChoose").split(",");

multiple hidden

first.jsp

<select name='chosen' multiple>
  ...
</select>
<input type='hidden' name='toChoose' value='1'/>
<input type='hidden' name='toChoose' value='2'/>
....

next.jsp

String[] chosen = request.getParameterValues("chosen");
String[] toChoose = request.getParameterValues("toChoose");

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