Question

I intend to pass javascript array to the server. this array will basically contain all the option values of a select tag with multiple option.

Client side

<input type="hidden" id="selectedGroupIds" name="selectedGroupIds">
<input type="submit" name="mapSubmit" value="Map Now" onclick="setGroupIds()">

function setGroupIds() {
    var selectedGroupIds = [];
    $('#selectedGroups option').each (function() {
        selectedGroupIds.push($(this).val());
    });
    $('#selectedGroupIds').val(selectedGroupIds);
}

Server side

String[] groupArr = request.getParameterValues("selectedGroupIds");
System.out.println("Length = " + groupArr.length); // Prints 1 even if 2 elements in the array like [1,2]

Update I knw it can be done by getParameter() and split. Just curious to know if can be done without split by using getParameterValues()

Était-ce utile?

La solution 2

get parameter values is to be used when you have a single parameter with multiple values. You think you have accomplished this by setting your array as the val() of the input field, however you have actually done an implicit join (or implode) type command on your array into a single string, a comma delimited list, so getparametervalues will always just get one value.

a situation to use it to get multiple values, would be something like

<input type=checkbox name=check1 value='1'>
<input type=checkbox name=check1 value='2'>
<input type=checkbox name=check1 value='3'>

to get all the different values checked for the group of checkboxes called check1

Autres conseils

String selectedGroups = request.getParameter("selectedGroupIds");
String[] arr = (selectedGroups!=null)?selectedGroups.split(","):null;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top