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
})
最佳答案
有关所选选项的所有信息都包含函数的 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
});
关于javascript - select2 获取触发选择/取消选择的选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819709/