引导选择 - 当我将选择选择器类添加到选择标签时,下拉列表消失了



当我将selectpicker类添加到我的选择标签并在浏览器中运行我的页面时,它没有显示任何<select>框...
Detail,当我删除selectpicker类时,它运行正常。无论如何,可以在代码源中看到选择框。

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.3/css/bootstrap-select.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.3/js/bootstrap-select.min.js"></script>
<!-- (Optional) Latest compiled and minified JavaScript translation files -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.3/js/i18n/defaults-*.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<select class="selectpicker" data-live-search="true" name="cidade">
<option value="test1">Test 1</option>
<option value="test2">Test 2</option>
<option value="test3">Test 3</option>
</select>
</body>

我遇到了类似的问题。通过对我的控制器的AJAX请求,返回了带有HTML的字符串,但我无法将其附加到DOM。然而,在检查时,它出现在DOM中。原来,问题出在selectpicker类。删除后,已成功追加HTML,并且元素自然未按预期运行。 经过一些搜索,我遇到了以下解决方案:

$('.selectpicker').selectpicker('refresh')

在这里找到它,并解释为什么会发生这种情况。 希望这有帮助。

干杯!

把你的 JQuery 脚本放在你的 Bootstrap 脚本之前.
它为我修复了它。

<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.14.0-beta2/js/bootstrap-select.min.js" integrity="sha512-FHZVRMUW9FsXobt+ONiix6Z0tIkxvQfxtCSirkKc5Sb4TKHmqq1dZa8DphF0XqKb3ldLu/wgMa8mT6uXiLlRlw=="crossorigin="anonymous" referrerpolicy="no-referrer"></script>

如果使用 bootstrap 3.3.7,则必须包含 JQuery 作为引导插件。 喜欢这个

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>
<!-- other javascript inludes follows -->

这是我在class=selectpicker时对选择进行不了解的解决方案。当我动态添加引导选择时,我的问题发生了。经过一天的搜索,我找到了我的解决方案。我没有在网上看到这个解决方案。

在附加选择项后,必须使用 .selectpicker 命令。这是我下面的代码。

我希望这个解决方案对每个人都有用。

var selectLocaleList = document.createElement("select");
selectLocaleList.setAttribute("id", "myLocaleSelect");
selectLocaleList.setAttribute("class", "form-control");
selectLocaleList.setAttribute("data-style", "btn-danger");
//Create and append the options
$(selectLocaleList).append('<option value="tr">Turkish</option>');
$(selectLocaleList).append('<option value="en">English</option>');
//This is the append code below that adds the select to the page. And this code must be ovet the .selectpicker 
modalBody.appendChild(selectLocaleList);
//And this is the .selectpicker after the append..
$(selectLocaleList).selectpicker('val', settings.locale)
.on('change', function () {
// Some functions
});

你需要在 Bootstrap 和 bootstrap-select之前包含 jQuery。此外,您需要在所有 Bootstrap 文件之后包含所有引导选择文件(在您的代码中,您在 Bootstrap CSS 之前包含引导选择 CSS(。

如果您动态填充<option>,我的建议是在完成附加部分后刷新.selectpicker$(".selectpicker").selectpicker('refresh');

最新更新