https://select2.github.io/options.html#what-events-will-select2-trigger
文档说明可以阻止select2:selecting
和select2:unselecting
。
我想根据选择/取消选择的值有条件地阻止事件。
$('select').on('select2:selecting', function(e) {
// what value is currently being selected?
// $(this).val() does not yet have it and I should set it conditionally
})
答案 0 :(得分:1)
有关所选选项的所有信息都包含您的函数的e
变量,因此您可以这样:
$('select').select2().on('select2:selecting', function(e) {
console.log(e.params.args.data.id); //value of selected option
console.log(e.params.args.data.text); // text of selected option
});