16

如何使用Select2获取未选择选项的值select2:unselect

$('#mySelect').on("select2:unselect", function(e){

    var unselected_value = $('#mySelect').val(); // using this shows 'null'
    // or using below expression also shows 'null'
    var unselected_value = $('#mySelect :selected').val();

    alert(unselected_value);
}).trigger('change');

在上面的代码警报中显示'null'

我需要使用select2:unselect,因为“更改”事件会感知:select:unselect两者。

4

9 回答 9

14

实际上,数据值在事件对象内部。如果您正在处理 select2 多个值,这将很有用。

function(e){
    console.log(e.params.data)
}
于 2016-03-24T15:52:08.207 回答
14

这是一个较老的问题,但也许这个答案会对某人有所帮助。我正在使用带有多个值/标签的 Select2 v4.0.3,并且只需要删除特定的 ID。我真的不想使用unselecting其他答案中提到的事件。如果unselect没有args对象,那么您可以像这样获取您尝试删除的对象的 ID...

jQuery('.mySelect2').on("select2:unselecting", function(e){
    return true; // I don't use this unselecting event but here is how you could use it to get the ID of the one you are trying to remove.
    console.log(e);
    console.log(e.params);
    console.log(e.params.args.data);
    console.log(e.params.args.data.id);
    //console.log(e.params.args.data.tag); data.tag is specific to my code.
});

jQuery('.mySelect2').on('select2:unselect', function (e) {
    console.log(e);
    console.log(e.params);
    console.log(e.params.data);
    console.log(e.params.data.id);
    //console.log(e.params.data.tag); data.tag is specific to my code.

    /*
    Now you can do an ajax call or whatever you want for the specific
    value/tag that you are trying to remove which is: e.params.data.id.
    */
于 2017-01-13T19:12:13.130 回答
8

最后,我得到了解决方案,那就是:未选择选项的值仅在未选择之前可用。不是select2:unselect现在select2:unselecting ,代码将是:

$('#mySelect').on("select2:unselecting", function(e){
         var unselected_value = $('#mySelect').val();
         alert(unselected_value);
    }).trigger('change');
于 2015-12-25T15:32:42.467 回答
7

未选择的元素正在通过 e.params.data。我们可以使用“id”访问它的值,如果需要文本,可以使用“data.text”。

 $("select").on("select2:unselect", function (e) {
             var value=   e.params.data.id;
             alert(value);
    });
于 2018-02-28T13:42:08.320 回答
0

我发现在 Select2 4.0.3 上e.params已更改。因此,要从取消选择项目中查找 id,请更改您的代码,如下所示:

$('select').on('select2:unselecting', function (e) {
    var id = e.params.args.data.id; //your id
    alert('Are You Sure ?');
    e.preventDefault();
  // Do something with your id
});
于 2017-08-15T11:18:41.820 回答
0

如果您使用多个标签,则获取特定的选项值:

var unselected_value = $('#mySelect option:selected').val();
alert(unselected_value);
于 2017-04-08T02:29:45.113 回答
0

这是我在 SVG 地图中更改城市背景颜色以查找特定城市名称的解决方案。希望这可以帮助你

 $(".js-example-diacritics").on("select2:unselect", function(evt) {
                var element = evt.params.data.element;
                townColor(element.text, unHighLightColor);
            });

我将未选中的 select2 元素的文本(城市名称)和名为 unHighLightColor 的常量传递给方法 townColor(),其中包含颜色名称。

于 2017-04-13T18:41:48.017 回答
0

使用 $(this).val(),这将返回选择中标记的最后一个值

$('#mySelect').on("select2:unselecting", function(e){
         alert($(this).val());
    })
于 2019-07-01T13:35:21.883 回答
0

@Mahmaood ali 感谢您的解决方案,它对我来说就像一个魅力。

对于@Vipin Kr。辛格问题。

  $('#mySelect').on("select2:unselect", function(e){
        var unselected_value = e.params.data.id;
    })
    $('#mySelect').on("select2:select", function(e){
        var selected_value = e.params.data.id;
    })

这样,只有您一次只能获得一个选定或未选定的值。

于 2020-11-17T00:12:37.983 回答