无法检索select2上选择的值:选择

时间:2017-01-05 15:46:55

标签: jquery select2 jquery-select2-4

我使用 Select2 4.0.3 进行单选。

我试图在触发更改事件之前获取要选择的值(用户选择并将被分配的值)。

这是我用来挂钩事件和我测试的代码:

$('#mycombo').on("select2:selecting", function (evt) {

  var $this = $(this);

  console.log($this.val()); // This return the current value
  console.log(evt.data);    // This return unassigned
  console.log(evt.params);  // This return unassigned
  console.log(evt.args);    // This return unassigned
  console.log(evt.object);  // This return unassigned      
})

我也在这里尝试了Fire callback when selection was made with select2 4.0, and retrieve the value of last selection示例,但看起来同时触发了select和change事件。

我迷失了,任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:2)

您可以拥有参数中的信息,例如尝试使用此

$('#mycombo').on("select2:selecting", function (evt) {
  console.log(evt.params.args.data);  // this returns the object of 
})

然后在数据中你可以做类似.text或.id的事情,你可以分别获得选项或值的文本。

evt.params.args.data.text
相关问题