Javascript 使用 Select2 得到错误 - “错误:没有 select2/compat/query”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30235909/
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
Using Select2 get error - "Error: No select2/compat/query"
提问by Simon
I am attempting to use the Select2library in my site to leverage placecomplete(following it's rather simple configuration steps here), but when I run the code I get a strange error -
我试图在我的站点中使用Select2库来利用placecomplete(遵循这里相当简单的配置步骤),但是当我运行代码时,我收到一个奇怪的错误 -
Error: No select2/compat/query`
错误:没有 select2/compat/query`
I've tried googling it and it really seems like no one else has encountered this?
我试过在谷歌上搜索它,似乎真的没有其他人遇到过这种情况?
My code in a nutshell is,
简而言之,我的代码是,
loading the files...
正在加载文件...
<link href="scripts/Select2/dist/css/select2.min.css" rel="stylesheet" />
<script src="scripts/Select2/dist/js/select2.min.js"></script>
<script src="scripts/jquery.placecomplete.js">//<![CDATA[//]]></script>
Tagging an input element in my body...
在我的身体中标记一个输入元素......
<input id="example123" class="example123" type="text" />
In my document.ready function, calling the following code...
在我的 document.ready 函数中,调用以下代码...
$('#example123').placecomplete({});
and thats where it throws the error.
这就是它抛出错误的地方。
Any ideas where Im going wrong, and what this error means?
我哪里出错了,这个错误意味着什么?
回答by Kevin Brown
You are running into two issues here, both of which can easily be fixed.
您在这里遇到了两个问题,这两个问题都可以轻松解决。
Select2 4.0.0 no longer supports the
queryoption in the slimmed down, standard build. This must be included in the full build (select2.full.js) as it is handled through a backwards compatibility module.You are using Placecomplete and it depends on an older version of Select2. It looks like Select2 3.5.2+ can work, but I can tell from the options that it is using, it cannot work with Select2 4.0.0. There is an open ticketabout this for Placecomplete.
回答by Liam
I've had this when you have two controls on your web page with the same Id - I had a hidden control and a select (the hidden control was first).
当您的网页上有两个具有相同 Id 的控件时,我就遇到过这种情况 - 我有一个隐藏控件和一个选择(隐藏控件是第一个)。
回答by Payam Khaninejad
For me, changing the <input />tag to a <select>tag worked.
对我来说,将<input />标签更改为<select>标签有效。

