how can I get value of unselected option in Select2 using 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'
在上面的代码警报显示'null'
I need to use select2:unselect
because 'change' event will sense :select
and :unselect
both.
我需要使用select2:unselect因为'change'事件会感知:select和:取消选择两者。
7 个解决方案
#1
12
Actually, the data value is inside the event Object. It would be useful if you are dealing with select2 multiple values.
实际上,数据值在事件Object内。如果您正在处理select2多个值,那将非常有用。
function(e){
console.log(e.params.data)
}
#2
4
Finally, I've got the solution and that is: The value of unselected option is only available before it is unselected. Not at select2:unselect
rather at select2:unselecting
now the code will be:
最后,我得到了解决方案,即:未选中的选项的值仅在取消选中之前可用。不在select2:取消选择而不是在select2:取消选择现在代码将是:
$('#mySelect').on("select2:unselecting", function(e){
var unselected_value = $('#mySelect').val();
alert(unselected_value);
}).trigger('change');
#3
4
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 unselecting
event as mentioned in other answers. In the unselect
event there is no args
object, so you can get to the ID of the one you are trying to remove like this...
这是一个较老的问题,但也许这个答案会对某人有所帮助。我正在使用带有多个值/标签的Select2 v4.0.3,并且只需删除特定ID的ID。我真的不想使用其他答案中提到的未选择事件。在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.
*/
#4
1
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);
});
#5
0
Getting the specific option value if you're using multiple tags:
如果您使用多个标记,请获取特定选项值:
var unselected_value = $('#mySelect option:selected').val();
alert(unselected_value);
#6
0
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的常量。
#7
0
on Select2 4.0.3 i'am found e.params
was 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
});
#1
12
Actually, the data value is inside the event Object. It would be useful if you are dealing with select2 multiple values.
实际上,数据值在事件Object内。如果您正在处理select2多个值,那将非常有用。
function(e){
console.log(e.params.data)
}
#2
4
Finally, I've got the solution and that is: The value of unselected option is only available before it is unselected. Not at select2:unselect
rather at select2:unselecting
now the code will be:
最后,我得到了解决方案,即:未选中的选项的值仅在取消选中之前可用。不在select2:取消选择而不是在select2:取消选择现在代码将是:
$('#mySelect').on("select2:unselecting", function(e){
var unselected_value = $('#mySelect').val();
alert(unselected_value);
}).trigger('change');
#3
4
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 unselecting
event as mentioned in other answers. In the unselect
event there is no args
object, so you can get to the ID of the one you are trying to remove like this...
这是一个较老的问题,但也许这个答案会对某人有所帮助。我正在使用带有多个值/标签的Select2 v4.0.3,并且只需删除特定ID的ID。我真的不想使用其他答案中提到的未选择事件。在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.
*/
#4
1
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);
});
#5
0
Getting the specific option value if you're using multiple tags:
如果您使用多个标记,请获取特定选项值:
var unselected_value = $('#mySelect option:selected').val();
alert(unselected_value);
#6
0
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的常量。
#7
0
on Select2 4.0.3 i'am found e.params
was 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
});