json data:
["string 1","string 2","test 07 ","here is test"]
and this is html
<select class="selectpicker" id="ddl_location">
<option select="selected">Select Location </option>
</select>
and here is JQuery code
for (var i = 0; i < jsonData.length; i++) {
$("#ddl_location").append('<option value="' + jsonData[i] + '">' + jsonData[i] + '</option>');
}
the issue is that
data is not populated in drop-down list
after working around the issue, the solution was :
var jsonData = JSON.stringify(data);
$.each(JSON.parse(jsonData), function (idx, obj) {
$("#ddl_location").append('<option value="' + obj.id + '">' + obj.location + '</option>').selectpicker('refresh');
});
step 1: stringify received object
step 2: use each function instead of for loop
step 3: parse the data using JSON.parse(jsonData )
step 4: refresh drop-down list every time you append data to drop-down list using .selectpicker('refresh')
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