javascript select2-selecting 事件没有被触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17550813/
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
select2-selecting event not getting fired
提问by coder
I am using Select2in my website, and I'm trying to use the select2-selecting
event, but its not firing. I am also using Backbone.jsin the app, so the first thing I tried was adding the select2-selecting
to my events
object:
我在我的网站上使用Select2,我正在尝试使用该select2-selecting
事件,但它没有触发。我也在应用程序中使用Backbone.js,所以我尝试的第一件事是将 加入select2-selecting
到我的events
对象中:
// 'change .city':'cityChanged'
'select2-selecting .city':'cityChanged'
Note that I have a change
event commented out - this change
event works properly. In the documentation for Select2, the select2-selecting
event is put directly on the object, not like this:
请注意,我已change
注释掉一个事件 - 此change
事件正常工作。在Select2的文档中,select2-selecting
事件直接放在对象上,而不是这样:
$('.city').select2().on('select2-selecting', function(e){
console.log('here');
});
instead, its supposed to be used like this:
相反,它应该像这样使用:
$('.city').on('select2-selecting', function(e){
console.log('here');
});
I have also tried adding the event both of these ways, but the event didn't fire (I did check and the element was created on the DOM beforeI added the events).
我也试过用这两种方式添加事件,但事件没有触发(我确实检查过并且在添加事件之前在 DOM 上创建了元素)。
When I add the event in the first method with the Backbone.js, the event is listed in the event listeners in the chrome debug console - it just doesn't get fired. Does anyone have an idea what is going on?
当我使用Backbone.js在第一种方法中添加事件时,该事件列在 chrome 调试控制台的事件侦听器中 - 它只是不会被触发。有谁知道发生了什么?
回答by Jmirancid Pes
what version of select2
are you using?
select2
你用的是什么版本?
I was having the same problem until I realize I was using the 3.3 version where this select2-selecting
event not exists.
我遇到了同样的问题,直到我意识到我使用的是select2-selecting
不存在此事件的 3.3 版本。
This has been included in the 3.4 version.
这已包含在 3.4 版本中。
回答by Santi Iglesias
There was a change on earlier versions also where it changes name:
早期版本也发生了变化,它更改了名称:
- select2-close is now select2:close
- select2-open is now select2:open
- select2-opening is now select2:opening
- select2-selecting is now select2:selecting
- select2-removed is now select2:removed
- select2-removing is now select2:unselecting
- select2-close 现在是 select2:close
- select2-open 现在是 select2:open
- select2-opening 现在是 select2:opening
- select2-selecting 现在是 select2:selecting
- select2-removed 现在是 select2:removed
- select2-removing 现在是 select2:unselecting
回答by Kaique Garcia
On even older versions, 'select2-removed' and 'select2-removing' events listed on @santi-iglesias' answer doesn't exists. You have 'removed' instead. Also, to get the affected optioin value, use 'event.val'.
在更旧的版本上,@santi-iglesias答案中列出的“ select2-removed”和“ select2-removing”事件不存在。你已经“删除”了。此外,要获得受影响的选项值,请使用“ event.val”。
So you can do something like this:
所以你可以做这样的事情:
$('.select').on('select2-selecting removed', function(evt) {
var value = evt.val; //do something with this
});
Checked at v3.4.3.
在 v3.4.3 检查。