简体   繁体   English

“未为 Select2 未定义错误定义查询函数”

[英]“query function not defined for Select2 undefined error”

尝试使用 Select2 并在多个项目输入/文本字段上收到此错误:

"query function not defined for Select2 undefined error"

Covered in this google group thread 涵盖在此谷歌群组线程中

The problem was because of the extra div that was being added by the select2.问题是因为 select2 添加了额外的 div。 Select2 had added new div with class "select2-container form-select" to wrap the select created. Select2 添加了带有类“select2-container form-select”的新 div 来包装创建的选择。 So the next time i loaded the function, the error was being thrown as select2 was being attached to the div element.所以下次我加载函数时,错误被抛出,因为 select2 被附加到 div 元素。 I changed my selector...我改变了我的选择器...

Prefix select2 css identifier with specific tag name "select":使用特定标签名称“select”为 select2 css 标识符添加前缀:

$('select.form-select').select2();

This error message is too general.这个错误信息太笼统了。 One of its other possible sources is that you're trying to call select2() method on already "select2ed" input.它的其他可能来源之一是您试图在已经“select2ed”的输入上调用select2()方法。

In case you initialize an empty input do this:如果您初始化空输入,请执行以下操作:

$(".yourelement").select2({
 data: {
  id: "",
  text: ""
 }
});

Read the first comment below, it explains why and when you should use the code in my answer.阅读下面的第一条评论,它解释了为什么以及何时应该使用我的答案中的代码。

我也遇到了这个问题,请确保您没有将 select2 初始化两次。

For me this issue boiled down to setting the correct data-ui-select2 attribute:对我来说,这个问题归结为设置正确的 data-ui-select2 属性:

<input type="text" data-ui-select2="select2Options.projectManagers" placeholder="Project Manager" ng-model="selectedProjectManager">


$scope.projectManagers = { 
  data: []  //Must have data property 
}

$scope.selectedProjectManager = {};

If I take off the data property on $scope.projectManagers I get this error.如果我取消$scope.projectManagers上的data属性,我会收到此错误。

This issue boiled down to how I was building my select2 select box.这个问题归结为我如何构建我的 select2 选择框。 In one javascript file I had...在一个 javascript 文件中,我有...

$(function(){
  $(".select2").select2();
});

And in another js file an override...在另一个 js 文件中覆盖...

$(function(){
    var employerStateSelector = 
                $("#registration_employer_state").select2("destroy");
    employerStateSelector.select2({
    placeholder: 'Select a State...'
    });
});

Moving the second override into a window load event resolved the issue.将第二个覆盖移动到窗口加载事件中解决了该问题。

$( window ).load(function() {
  var employerStateSelector = 
              $("#registration_employer_state").select2("destroy");
  employerStateSelector.select2({
    placeholder: 'Select a State...'
  });
});

This issue blossomed inside a Rails application这个问题在 Rails 应用程序中出现了

I also got the same error when using ajax with a textbox then i solve it by remove class select2 of textbox and setup select2 by id like:在将 ajax 与文本框一起使用时,我也遇到了同样的错误,然后我通过删除文本框的类 select2并通过 id 设置 select2 来解决它,例如:

$(function(){
  $("#input-select2").select2();
});

It seems that your selector returns an undefined element (Therefore undefined error is returned)您的选择器似乎返回了一个未定义的元素(因此返回了undefined error

In case the element really exists, you are calling select2 on an input element without supplying anything to select2, where it should fetch the data from.如果元素确实存在,您将在input元素上调用 select2 而不向 select2 提供任何内容,它应该从中获取数据。 Typically, one calls .select2({data: [{id:"firstid", text:"firsttext"}]) .通常,调用.select2({data: [{id:"firstid", text:"firsttext"}])

Also got the same error when using ajax.使用ajax时也出现同样的错误。

If you're using ajax to render forms with select2, the input_html class must be different from those NOT rendered using ajax.如果您使用 ajax 来呈现带有 select2 的表单,则 input_html 类必须与那些未使用 ajax 呈现的类不同。 Not quite sure why it works this way though.不太确定为什么它会以这种方式工作。

if (typeof(opts.query) !== "function") {
    throw "query function not defined for Select2 " + opts.element.attr("id");
}

This is thrown becase query does not exist in options.这是因为选项中不存在查询而引发的。 Internally there is a check maintained which requires either of the following for parameters内部有一个检查维护,需要以下任一参数

  • ajax阿贾克斯
  • tags标签
  • data数据
  • query询问

So you just need to provide one of these 4 options to select2 and it should work as expected.因此,您只需要为 select2 提供这 4 个选项之一,它就会按预期工作。

I got the same error.我得到了同样的错误。 I have been using select2-3.5.2我一直在用select2-3.5.2

This was my code which had error这是我的代码有错误

    $('#carstatus-select').select2().val([1,2])

Below code fixed the issue.下面的代码解决了这个问题。

    $('#carstatus-select').val([1,2]);

I have a complicated Web App and I couldn't figure out exactly why this error was being thrown.我有一个复杂的 Web 应用程序,我无法弄清楚为什么会抛出这个错误。 It was causing the JavaScript to abort when thrown.它导致 JavaScript 在抛出时中止。

In select2.js I changed:在 select2.js 我改变了:

        if (typeof(opts.query) !== "function") {
            throw "query function not defined for Select2 " + opts.element.attr("id");
        }

to:到:

        if (typeof(opts.query) !== "function") {
            console.error("query function not defined for Select2 " + opts.element.attr("id"));
        }

Now everything seems to work properly but it is still logging in error in case I want to try and figure out what exactly in my code is causing the error.现在一切似乎都正常工作,但它仍然记录错误,以防我想尝试找出我的代码中究竟是什么导致了错误。 But for now this is a good enough fix for me.但就目前而言,这对我来说已经足够好了。

use :采用 :

try {
    $("#input-select2").select2();
}
catch(err) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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