Select2 get选项值,触发选择/取消选择

时间:2017-06-29 08:15:18

标签: javascript jquery jquery-select2

https://select2.github.io/options.html#what-events-will-select2-trigger

文档说明可以阻止select2:selectingselect2: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
})

1 个答案:

答案 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
});

Check this jsFiddle