繁体   English   中英

如何获取 select2:unselect 的值

[英]How to get value of select2:unselect

如何使用select2:unselectSelect2中获取未选择选项的值

$('#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');

在上面的代码警报中显示“空”

我需要使用select2:unselect因为 'change' 事件会感知:select:unselect两者。

这是一个较旧的问题,但也许这个答案会对某人有所帮助。 我正在使用具有多个值/标签的 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.
    */

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

function(e){
    console.log(e.params.data)
}

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

$('#mySelect').on("select2:unselecting", function(e){
         var unselected_value = $('#mySelect').val();
         alert(unselected_value);
    }).trigger('change');

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

 $("select").on("select2:unselect", function (e) {
             var value=   e.params.data.id;
             alert(value);
    });

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

var unselected_value = $('#mySelect option:selected').val();
alert(unselected_value);

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

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

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

在 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
});

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

$('#mySelect').on("select2:unselecting", function(e){
         alert($(this).val());
    })

@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;
    })

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM