I want to be able to send all the selected values from a listbox as a hidden field using JQuery. 开发者_JS百科I was able to send/retrieve a single value on form submission.
If anyone can share some code snippet, that would help.
Thanks in Advance!
Well, it's not so clear what you are really asking for. There is no "multiple selection dropdownlist" in HTML. To have a multiselect you need to specify
<select id="foobar" multiple>
that will create a listbox
where you may select multiple elements. Calling
var sel = $('#foobar').val();
will return an Array
of selected items.
edit
To get the text from each option, use .map()
or jQuery.map()
. Example:
var sel = $('#foobar').children('option:selected').map(function(i,e){
return e.innerText;
}).get();
That will create an Array containing the text of all selected entrys.
There's absolutely no need to do like that. You was apparently using request.getParameter()
instead of request.getParameterValues()
and wondering why it returned only the first value.
Just fix your servlet code accordingly:
String[] selectedItems = request.getParameterValues("dropdownname");
No need for ugly JS/jQuery hacks to send them all as single parameter. In future questions, try to ask how to solve a problem instead of how to achieve a solution which may after all not be the right solution per se.
精彩评论