简体   繁体   中英

Not able to populate multiselect dropdown

I am trying to dynamically populate the options of a multiselect dropdown. Below is my code:

<label for="serviceTypeCntrl" style="padding-left: 14px;">Service Type : </label>
<select id="serviceTypeCntrl" name="serviceTypeCntrl" class="selectpicker" multiple="multiple">
</select>

Then I am trying to populate the dropdown dynamically using ajax call.

$(document).ready(function () {         
    $.ajax({            
        url : 'ServicerServlet?identifier=PopulateServiceType',
        type : 'post',
        dataType: 'json',
        success : function(responseText) {
            alert(responseText);
            //var option="";
            $("#serviceTypeCntrl").find("option").remove();
            responseText.forEach(function(serviceType) {
            //option = $('<option>' + serviceType + '</option>');
            //$('#serviceTypeCntrl').append(option);
            //$('<option>').text(serviceType).appendTo('#serviceTypeCntrl');
            $('#serviceTypeCntrl').append($('<option>').text(serviceType)); 
            })
        }        
   });
});

Value is coming from the servlet but the dropdown is not getting populated. What am I doing wrong here? Looking forward to your answers.

Thanks in advance.

Try once like this

$('#serviceTypeCntrl').append("<option>" + serviceType + "</option>"));

instead of this

$('#serviceTypeCntrl').append($('<option>').text(serviceType));  

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM