javascript 为什么 select2-removing 事件没有在 select2 中使用 allowClear 触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24558101/
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
Why does the select2-removing event not trigger in select2 with allowClear?
提问by k0pernikus
I want to hook an event in my select2once it gets cleared. The select2 was initalized with allowClear: true
. Yet the event
一旦它被清除,我想在我的select2 中挂钩一个事件。select2 被初始化为allowClear: true
. 然而事件
$select.on('select2-removed', function(event) {
console.log(event);
});
does not trigger when resetting the select2 with the clear button.
使用清除按钮重置 select2 时不会触发。
采纳答案by Magus
According to the select2 source code, the clear
method use the event select2-clearing
instead of select2-removing
. So you should listen to this event too.
根据 select2 源代码,该clear
方法使用事件select2-clearing
而不是select2-removing
. 所以你也应该听听这个事件。
select2-clearing
and select2-removing
are triggered beforethe removal. select2-removed
is triggered after.
select2-clearing
并在移除前select2-removing
触发。之后触发。select2-removed
Keep in mind that clear
does not always trigger the select2-removed
event. It look like this event is triggered only if an element is actually removed from the input.
请记住,clear
这并不总是会触发select2-removed
事件。看起来只有在实际从输入中删除元素时才会触发此事件。
回答by Sadee
select2 v4.0.3
选择2 v4.0.3
$('#smartsearch').on('select2:unselecting', function (e) {
alert('You clicked on X');
});
For all the select2 Options & Events
对于所有select2 选项和事件
回答by HaReL
For me the only thing that worked is the 'select2:unselect'
event... (In non-multiple select box)
对我来说唯一有效的是'select2:unselect'
事件......(在非多选框中)
回答by k0pernikus
I got it working with the 'select2-removed'
-event instead of 'select2-removing'
when using the clear button.
我让它与'select2-removed'
-event一起工作,而不是'select2-removing'
在使用清除按钮时。
As to why it does not trigger still eludes me.
至于为什么它没有触发,我仍然不明白。
回答by Baqer Naqvi
I wanted to get data about the element just got removed on removed event. Following code worked for me;
我想获取有关在删除事件中刚刚删除的元素的数据。以下代码对我有用;
$('#selector').on('select2:unselect', function (e) {
var data = e.params.data;
});
回答by Aniket Shivam Tiwari
If you are working with 4 or above version then use
如果您使用的是 4 或更高版本,请使用
$('#id').on('select2:unselecting', function (e) {
alert('You clicked on X');
});
Below 4 version
4个版本以下
$('#id').on('select2-removing', function (e) {
alert('You clicked on X');
});
Make sure for 4 or above version it is select2:unselectingcolon(:)
Less than 4 version it is select2-removing-(hyphen)
确保对于 4 或更高版本它是select2:unselecting Colon(:)
小于 4 版本它是 select2-removing -(hyphen)