jQuery select2:TypeError:b.dataAdapter为null [英] jquery select2: TypeError: b.dataAdapter is null
问题描述
在使用select2 multiselect并选择一个选项时,显示此错误消息:
when using the select2 multiselect and selecting an option I get this error message displayed:
TypeError: b.dataAdapter is null
有人知道这是怎么回事吗?
Does anyone know what this is about?
多重选择效果很好,我只是想知道这个消息.
The multiselect works fine, I just wonder about this message.
这是我的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>
这是jquery的初始化:
this is the jquery init:
$(".select2me").select2({
placeholder:"Select",
width:"auto",
allowClear:!0
});
如果在另一个下拉菜单"projectSelector"中选择了值,则我获得的多选数据:
The data for the multiselect I get if a value is chosen in another dropdown '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
});
}
}
});
}
数据将被加载到多选中,一切都将在屏幕上按预期工作.只是在控制台中,我得到了错误消息
Data is getting loaded into the multiselect and everythings works as expected on the screen. Just in the console I get the error messsage of
TypeError: b.dataAdapter is null
这是我的json响应(简而言之):
This is my json response (in short):
{
"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"}
]
}
在Chrome浏览器中,我收到以下消息:
In Chrome I get this message:
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)
推荐答案
由于selector2元素倾向于存储一些其他数据,所以我注意到,除了$(".participantSelector").empty()
之外,清除了list元素的数据时,错误也消失了.因此,请尝试这种方式:
Since selector2 elements tend to store some additional data, I've noticed when the data of the list element is cleared in addition to $(".participantSelector").empty()
the error is gone. So, try it out this way:
var selector = $(".participantSelector");
selector.empty();
selector.removeData();
selector.select2({
placeholder: "Click here to select participants",
allowClear: false,
data: data
});
希望它也对您有用.
这篇关于jQuery select2:TypeError:b.dataAdapter为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!