我正在使用 Select2(4.00 版)并使用 ajax 方法加载远程数据。
我需要检索所选选项的标题,但在 select2:select 事件数据未定义
我的代码:
$(".js-data-action-terms").select2({
ajax: {
url: ajaxurl + "?action=terms",
dataType: 'json',
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return {
results: data.items
};
},
cache: false
},
escapeMarkup: function (markup) {
return markup;
},
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
$('.js-data-action-terms').on("select2:select", function(e) {
console.log(e);
});
结果日志:
最佳答案
在 Select2 4.0.0 中,所选对象从 evt.data
属性移至 evt.params.data
。现在,Select2 中事件的所有额外数据都放在 evt.params
中以保持一致性。
关于javascript - 使用 ajax 时,Select2(4.00) 在 onSelect 事件中返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208142/