Currently, I am utilizing the following code to create a searchable dropdown menu. I found helpful guidance in this forum post.
I am seeking advice on how to retrieve the value of the selected option. For example, if 'China' is chosen, I would like to assign the variable var country = 'CN'
.
My attempts thus far have been unsuccessful as most methods return 'US'. This may be due to 'United States' being the first option in the dropdown list.
Country:
<select class="form-control selectpicker" id="select-country" data-live-search="true">
<option disabled>Select a Country</option>
<option data-tokens="United States" value="US">United States</option>
<option data-tokens="United Kingdom" value="GB">United Kingdom</option>
<option data-tokens="China" value="CN">China</option>
<option data-tokens="Japan" value="JP">Japan</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css"></link>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.min.js"></script>
<script type="text/javascript">
$(function() {
$('.selectpicker').selectpicker();
});
</script>