Javascript $(...).selectpicker 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43365047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:52:27  来源:igfitidea点击:

$(...).selectpicker is not a function

javascriptjquerybootstrap-selectpicker

提问by Djov

I am using bootstrap-select for a form. I include the scripts (jquery, bootstrap-select) in the header of the HTML file.

我正在使用 bootstrap-select 作为表单。我在 HTML 文件的标题中包含了脚本(jquery、bootstrap-select)。

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
<link rel="stylesheet "type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

All the select elements with class "selectpicker" all called correctly. Example of the select element:

所有具有“selectpicker”类的选择元素都被正确调用。选择元素的示例:

<select id="test" class="selectpicker">
        <option>Mustard</option>
        <option>Ketchup</option>
        <option>Relish</option>
</select>

However, if I call the following script on the same page

但是,如果我在同一页面上调用以下脚本

<script>
$(document).ready(
    function () {
        $('#test').selectpicker('val', 'Relish')
});
</script>

I get this nasty error

我收到这个讨厌的错误

$(...).selectpicker is not a function

$(...).selectpicker 不是函数

Looking at the sources tab in Google Chrome, I see that the bootstrap-select.min.js is loaded well. Has anyone got suggestions?

查看 Google Chrome 中的源选项卡,我看到 bootstrap-select.min.js 加载良好。有人有建议吗?

回答by adSad

If you have another jquery.jsreference that is loading after bootstrap-select.min.jsit will wipe out $(...).selectpickerfunction and other functions. Make sure that bootstrap-select.min.js is loaded last.

如果您jquery.jsbootstrap-select.min.js之后加载了另一个引用,它将清除$(...).selectpicker函数和其他函数。确保 bootstrap-select.min.js 最后加载。

回答by Jo-Go

Try to load bootstrap before bootstrap-select ;-)

尝试在 bootstrap-select 之前加载 bootstrap ;-)

<link rel="stylesheet "type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>