引导选择DropDownList不显示

Bootstrap-select DropDownList not showing

本文关键字:显示 DropDownList 选择      更新时间:2023-09-26

我有一个引导选择组合框,我在ajax调用中用数据填充它Html:

<div class="row">
<select id="selectGemeente1" class="selectpicker" data-live-search="true"></select>
</div>

Ajax调用:

var gemeenteLijst = [];
var GetGemeentes = function () {
    $.ajax({
        type: "GET",
        dataType: "json",
        url: 'https://localhost::blabla',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var test = document.getElementById("selectGemeente1");
            data.forEach(function (item) {
                var opt = document.createElement("option");
                opt.value = item.GemeenteId;
                opt.innerHTML = item.Naam;
                test.appendChild(opt);
            });         
        },
        error: function (xhr, status, error) {
            alert(xhr.responseText);
        }
    });
}
$(document).ready(function () {
    GetGemeentes();  
})

运行我的应用程序后,我的选择被填充,但下拉列表没有打开。。

html运行后

我已经看到很多解决方案说,我应该把这个放在我的$(文档).准备

$(".selectpicker").html(optgroup);

但是我收到一个错误,selectpicker不是一个函数。

gemeenteLijstLaden.js:36 Uncaught TypeError: $(...).selectpicker is not a function

我已经实现了这些脚本文件

<script src="~/Scripts/jquery-2.2.3.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/i18n/defaults-nl_NL.min.js"></script>
<script src="~/Scripts/myCharts/gemeenteLijstLaden.js"></script>

我找到了一个解决方案。我只需要刷新我选择的

$('.selectpicker').selectpicker('refresh');
success: function (data) {   
  var selectGemeent = $("#selectGemeente1");
                selectGemeent.empty();
                $('#selectGemeente1').append($("<option></option>").
                                attr("value", "0").
                                text("Select"));
                $.each(data.forEach, function (index, item) {
                    selectGemeent.append($('<option>', {
                        value: item.GemeenteId,
                        text: item.Naam
                    })); ////function
                });
},

在ajax成功中使用它,并在文本和值之后添加其他文件