2
How can I recover the value of several dropdown (only filled)
The function below does not work correctly. For example, if I select only the second combo has as output: ,2 It picks up the void as if it were selection. It has a way around it?
$("div.container-fluid").on("change", "select.dropDown", function() {
var selectData = [];
$('select.dropDown option:selected').each(function(i, selected){
var selectData[i] = $(selected).val();
});
});
My html is like this:
<select class="dropDown" id="column_1">
<option value="" selected="" disabled="">Please select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
<select class="dropDown" id="column_2">
<option value="" selected="" disabled="">Please select</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
Sergio, could you explain why you don’t use: . filter(Function(i, Selected) in the second option? Another question: the "Selected" parameter is the dropdown selection in this case?
– Bruno Nascimento
@jQuery makes the iterated element available in two places. Kind of unnecessary but both the
this
and theselect
in case you use arguments(i, select)
in the iterator function. I chose to use thethis
, but if you use(i, selected)
is good anyway, even more according to the native javascript API. I answered your question?– Sergio