[英]Select2 off('change').on('change'…) does not able to change the selected option
I use Select2-4.0.0 And 我使用Select2-4.0.0和
$gameSelect.select2().on("change",function(e){....}
works fine. 工作良好。
But when I chain it following off('change') like: 但是,当我将其关闭后('更改'),如:
$gameSelect.select2().off('change').on("change",function(e){....}
The event is triggered but the selected item does not change at UI. 触发事件但所选项目在UI处不会更改。
Why is that? 这是为什么?
Looks like the select2-4.0 is adding its own change handlers to the select element, when you say off('change')
that too is getting removed that is the reason. 看起来select2-4.0正在将自己的更改处理程序添加到select元素,当你说off('change')
也被删除时,这就是原因。
To fix this kind of problems we have event name spacing so, use a namespace to your handlers and use it to remove them like 为了解决这类问题我们有事件名称间隔,所以,使用命名空间给你的处理程序并使用它来删除它们
$('#my-select').select2().off('change.mychange').on('change.mychange', function () {
//your code
});
To preserve the settings of select2 , use this code instead of the accepted solution : 要保留select2的设置 ,请使用此代码而不是接受的解决方案:
$('#my-select').off('change.mychange').on('change.mychange', function () {
//your code
});
You don't need to reinstatiate the .select2()
. 您无需重新.select2()
。 It's unnecessary and also shorter. 这是不必要的,也更短。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.