Javascript 如何获取 select2:unselect 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34463714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:22:26  来源:igfitidea点击:

How to get value of select2:unselect

javascriptjqueryselect2

提问by Vipin Kr. Singh

how can I get value of unselected option in Select2using select2:unselect

如何使用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');

in above code alert shows 'null'

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

I need to use select2:unselectbecause 'change' event will sense :selectand :unselectboth.

我需要使用,select2:unselect因为 'change' 事件会有意义,:select而且:unselect两者都有。

采纳答案by Vipin Kr. Singh

Finally, I've got the solution and that is: The value of unselected option is only available before it is unselected. Not at select2:unselectrather at select2:unselectingnow the code will be:

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

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

回答by Chun

Actually, the data value is inside the event Object. It would be useful if you are dealing with select2 multiple values.

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

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

回答by gfrobenius

This is an older question but maybe this answer will help someone. I'm using Select2 v4.0.3 with multiple values/tags and need only the ID of specific one being removed. I really did not want to use the unselectingevent as mentioned in other answers. In the unselectevent there is no argsobject, so you can get to the ID of the one you are trying to remove like this...

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

回答by Mahmaood ali

The unselected element is passing through e.params.data. We can access its value using 'id' and if you need the text you can use 'data.text'.

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

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

回答by Felipe Castillo

use $(this).val(), this going to return you the last value marked in the select

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

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

回答by Migu Wala Ngalan

Getting the specific option value if you're using multiple tags:

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

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

回答by Jcc.Sanabria

This is my solution to change the background color of a city in a SVG map looking for the name of the specific city. Hope this could help you

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

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

To the method townColor() I'm passing the text (city name) of the select2 element unselected and constant named unHighLightColor that holds the color name.

向方法townColor() 传递select2 元素的文本(城市名称)未选择和常量名为unHighLightColor 的保存颜色名称。

回答by wahmal

on Select2 4.0.3 i'am found e.paramswas changed. So, to find id from unselecting item, change your code like this :

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