I am getting the select dropdown options from the server, and i have stored a value in a variable in javascript. If the options are matching to the value i am having that will be preselected in the select dropdown. Below is my code.
this is to get the dropdown options from server and append to select dropdown.
$.ajax({
type: "GET",
url: '/enquiry/user',
dataSrc: "data",
dataType : 'json',
cache: false,
success: function(users){
var mySelect = $('#assigned');
$.each(users, function(index, options){
console.log(options._id);
mySelect.append(
$('<option></option>').val(options._id).html(options.firstName)
);
// $('#assigned').append('<option value="'+users._id+'">'+users.firstName+'</option>');
});
and here i am having a value in a variable
f8 = document.getElementById('assigned');
If the value in f8 matches to the options, that option should get pre-selected.
Iam using materialize CSS
Html dropdown <select id="assigned"> <option value="">Change Assignee</option> </select>
$('#optionID').prop('selected',true)
//to update material select component per materializecss doc
$('#select').material_select('destroy')
$('#select').material_select()
working fiddle here , though not ajax request, but dynamically generated options
edit:
$('#select').append($('<option></option>').val(VALUE).html(HTML))
$('#select').val(SELECTED_VALUE)
$('#select').material_select('destroy')
$('#select').material_select()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.