“query function not defined for Select2 undefined error”

前端 未结 13 1456
名媛妹妹
名媛妹妹 2020-11-28 04:22

Trying to use Select2 and getting this error on multiple item input/text field:

\"query function not defined for Select2 undefined error\"
相关标签:
13条回答
  • 2020-11-28 05:05

    I have a complicated Web App and I couldn't figure out exactly why this error was being thrown. It was causing the JavaScript to abort when thrown.

    In select2.js I changed:

            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.

    0 讨论(0)
  • 2020-11-28 05:06

    I also had this problem make sure that you don't initialize the select2 twice.

    0 讨论(0)
  • 2020-11-28 05:06

    This issue boiled down to how I was building my select2 select box. In one javascript file I had...

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

    And in another js file an override...

    $(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

    0 讨论(0)
  • 2020-11-28 05:11

    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:

    $(function(){
      $("#input-select2").select2();
    });
    
    0 讨论(0)
  • 2020-11-28 05:19

    Covered in this google group thread

    The problem was because of the extra div that was being added by the select2. Select2 had added new div with class "select2-container form-select" to wrap the select created. So the next time i loaded the function, the error was being thrown as select2 was being attached to the div element. I changed my selector...

    Prefix select2 css identifier with specific tag name "select":

    $('select.form-select').select2();
    
    0 讨论(0)
  • 2020-11-28 05:22

    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.

    0 讨论(0)
提交回复
热议问题