these are two select boxes with same name can any one guide mehow can I pass two select boxes values as one one single array
<select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">
<option value="">-- Choose Service Areas --</option>
<option value="1">all over `enter code here`Andhrapredesh</option>
<option value="11">asia</option>
<option value="12">North-South America</option>
</select>
<select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">`enter code here`
<option value="">-- Choose Service Areas --</option>
<option value="1">all over Andhrapredesh</option>
<option value="11">asia</option>
<option value="12">North-South America</option>
</select>
I hope this will help you.
$(document).on('change', 'select[name="servicetypes[]"]', function(){
var selectedValues = {};
$('select[name="servicetypes[]"]').each(function(){
var text = $(this).children("option").filter(":selected").text();
var value = $(this).val();
selectedValues[text] = value;
});
var textValue = $('#servicetypesArray').val(selectedValues);
alert(JSON.stringify(selectedValues, null, 4));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">
<option value="">-- Choose Service Areas --</option>
<option value="1">all over Andhrapredesh</option>
<option value="11">asia</option>
<option value="12">North-South America</option>
</select>
<select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">`enter code here`
<option value="">-- Choose Service Areas --</option>
<option value="1">all over Andhrapredesh</option>
<option value="11">asia</option>
<option value="12">North-South America</option>
</select>
<input type="text" id="servicetypesArray" name="data[servicetypes][]" value="" hidden/>
You will have your values in a multidimensional array named 'servicetypes'
Here is a CODEPEN Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With