javascript - How to make dropdown based one JSON, and selected parameter taken from another? -
so got js code, suppose make list of statuses in dropdown:
$.getjson('/statuses', { ajax: 'true' }, function (data) { var html; var len = data.length; html += '<select id="status" class="form-control selectpicker" width="150" style="width: 150px">'; (var = 0; < len; i++) { html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'; } html += '</select>'; $('#status').html(html); });
this part working, have make sure selected option equals other json. tried in other getjson:
status += '<select id="status" class="form-control selectpicker" width="150" style="width: 150px">'; status += '<option value="' + data.status + '" selected="selected">' + data.status + '</option>'; status += '</select>'
...it selects it, duplicates status. how can make proper way?
on second bit of code parsing new html, why duplicate. select option try this:
$('#status option[value="'+ data.status +'"]').prop('selected', true);
Comments
Post a Comment