“query function not defined for Select2 undefined error”

前端 未结 13 1455
名媛妹妹
名媛妹妹 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 04:57

    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.

    0 讨论(0)
  • 2020-11-28 04:57

    Also got the same error when using ajax.

    If you're using ajax to render forms with select2, the input_html class must be different from those NOT rendered using ajax. Not quite sure why it works this way though.

    0 讨论(0)
  • 2020-11-28 04:58

    I got the same error. I have been using 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]);
    
    0 讨论(0)
  • 2020-11-28 05:00

    For me this issue boiled down to setting the correct data-ui-select2 attribute:

    <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.

    0 讨论(0)
  • 2020-11-28 05:00
    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.

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

    use :

    try {
        $("#input-select2").select2();
    }
    catch(err) {
    
    }
    
    0 讨论(0)
提交回复
热议问题