Reputation: 153
HTML CODE
<select class="form-control" name="min_select[]">
<option value="15">15</option>
<option value="30">30</option>
</select>
JQuery Code
var val1[];
$('select[name="min_select[]"] option:selected').each(function() {
val1.push($(this).val());
});
when i run this code I get empty val array
Upvotes: 10
Views: 30057
Reputation: 343
This will also work
var val1= $("select[name=\'min_select[]\']").map(function() {
return $(this).val();
}).toArray();
Upvotes: 12
Reputation: 982
To get the selected value, whether it is multiselect or single select, use jQuery .val()
method.
If it is a multiselect, it will return an array of the selected values.
See jsfiddle for demo. Check console log
Upvotes: 1
Reputation: 2670
You can try the following
HTML
<select class="form-control min-select" name="min_select[]">
<option value="15">15</option>
<option value="30">30</option>
</select>
JQUERY
var values = [];
$("select.min-select").each(function(i, sel){
var selectedVal = $(sel).val();
values.push(selectedVal);
});
Is min_select[] a multiple choice select?
Upvotes: 1
Reputation: 1956
The declaration array syntax is in correct.Please check the below code
var val1=[];
$('select[name="min_select[]"] option:selected').each(function() {
val1.push($(this).val());
});
Upvotes: 7