jquery select2:TypeError:b.dataAdapter为null

use*_*112 5 javascript jquery select2

当使用select2 multiselect并选择一个选项时,我会显示以下错误消息:

TypeError: b.dataAdapter is null
Run Code Online (Sandbox Code Playgroud)

有谁知道这是关于什么的?

multiselect工作正常,我只是想知道这个消息.

编辑:

这是我的HTML:

<div class="form-group">
<label class="control-label col-md-12" for="participant-id">Participant    
<span class="required"> * </span>
</label>
<div class="col-md-12">
<input type="hidden" name="participant_id" value=""/>
<select name="participant_id[]" multiple="multiple" class="form-control select2me participantSelector" required="required" id="participant-id">
<option value=""></option>
</select>
</div>                                
</div>
Run Code Online (Sandbox Code Playgroud)

这是jquery init:

$(".select2me").select2({placeholder:"Select",width:"auto",allowClear:!0}))}
Run Code Online (Sandbox Code Playgroud)

如果在另一个下拉列表'projectSelector'中选择了一个值,我得到的多选数据:

$('.projectSelector').on('change', function() {
        var targetProject_id = $('#project-id :selected').val();
        updateParticpantDropdown(targetProject_id);
    });


function updateParticpantDropdown(selectedProjectId){
        $.ajax({
            type: "POST",
            url: '/xx/projects/xx/'+ selectedProjectId,
            dataType : "json",
            success: function (response, status) {
                if (response.result == "success"){
                    var data = response.data;
                    $(".participantSelector").empty().select2({
                        placeholder: "Click here to select participants",
                        allowClear: false,
                        data: data
                    });
                }
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

数据被加载到多选中,并且每个事件都在屏幕上按预期工作.只是在控制台中我得到了错误消息

TypeError:b.dataAdapter为null

这是我的json回复(简而言之):

    {"result":"success","data":[{"id":18,"text":"xx, Ana Rosa"},{"id":17,"text":"xx, Saul"},{"id":14,"text":"xx, Jesus"},{"id":15,"text":"xx, Jose Sergio"
},{"id":13,"text":"xx, Guadalupe"},{"id":12,"text":"xx, Adolfo"},{"id":25
,"text":"xx, Roland"},{"id":16,"text":"xx, Mariela Elisa"},{"id":11,"text":"xx, Roberto Carlos "},{"id":19,"text":"xx, Jose Rafael"},{"id":2,"text":"xx, Juan Carlos"}]}
Run Code Online (Sandbox Code Playgroud)

在Chrome中,我收到以下消息:

select2.full.min.js:2 
Uncaught TypeError: Cannot read property 'current' of null
    at HTMLSelectElement.<anonymous> (select2.full.min.js:2)
    at HTMLSelectElement.dispatch (jquery.min.js:3)
    at HTMLSelectElement.r.handle (jquery.min.js:3)
    at Object.trigger (jquery.min.js:3)
    at HTMLSelectElement.<anonymous> (jquery.min.js:3)
    at Function.each (jquery.min.js:2)
    at n.fn.init.each (jquery.min.js:2)
    at n.fn.init.trigger (jquery.min.js:3)
    at d.select (select2.full.min.js:1)
    at d.select (select2.full.min.js:2)
Run Code Online (Sandbox Code Playgroud)

Ole*_*rov 4

由于 Selector2 元素往往会存储一些额外的数据,因此我注意到当列表元素的数据被清除时,$(".participantSelector").empty()错误也消失了。所以,尝试一下这样:

var selector = $(".participantSelector");
selector.empty();
selector.removeData();
selector.select2({
    placeholder: "Click here to select participants",
    allowClear: false,
    data: data
});
Run Code Online (Sandbox Code Playgroud)

希望它也适合你。