2015-10-19 106 views
4

我正在使用Select2(ver 4.00)并使用ajax方法加载远程数据。Select2(4.00)在使用ajax时在onSelect事件中返回undefined

我需要检索所选选项的称号,但在选择2:选择事件数据undifined

我的代码:

$(".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); 
}); 

结果日志:

enter image description here

回答

4

在选择二4.0.0,所选对象已从evt.data属性移动到evt.params.data。现在Select2中所有额外的事件数据被放入evt.params以保持一致性。

+0

谢谢!这就是我正在寻找的! – Dzhuang