简体   繁体   English

选择 select2 的 allowClear 选项时如何捕获事件?

[英]How to capture event when allowClear option of select2 is chosen?

I've tried to capture select2:clearing event我试图捕获select2:clearing事件

$("#my-select").on("select2:clearing", function (e) {
    console.log('cleared');
});

( jsfiddle ) ( jsfiddle )

but it is not fired.但它没有被解雇。 I've also tried other versions (like select2:removed etc., see another question with similar problem), but it also doesn't work.我也尝试过其他版本(如select2:removed等,请参阅另一个有类似问题的问题),但它也不起作用。

I use select2 4.0.0-beta2.我使用 select2 4.0.0-beta2。

The changelog of select2 4.0.0-beta2 states: select2 4.0.0-beta2 的更新日志指出:

Removed events删除的事件

select2-clearing - Use select2:unselecting instead

https://github.com/select2/select2/releases https://github.com/select2/select2/releases

This works:这有效:

$("#my-select").on("select2:unselecting", function(e) {

 });
$("#my-select").on("select2:select select2:unselecting", function(e) {
   //do something here
 });

According to the select2 event documentations , all of the unselect, unselecting, clear and clearing events will works !根据 select2事件文档,所有取消选择、取消选择、清除和清除事件都将起作用!

But consider that usnselect or unselecting events should be used by single selection mode, and clear or clearing should be used by multi selection mode.但考虑到usnselect取消选择事件应该由单一选择模式中使用,并清除清除多选择模式中使用。 So the clear or clearing will fires if all of selected items cleared or clearing.因此,如果所有选定的项目都被清除或清除,清除或清除将被触发。

The differences between unselect and unselecting, is that unselecting fires before removing selection so you can prevented it.取消选择和取消选择之间的区别在于取消选择会在删除选择之前触发,因此您可以阻止它。

Hope that it helps.希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在jquery UI对话框上使用select2(v 4.0)时,如何在使用远程数据源时使allowClear选项起作用? - When using select2 (v 4.0) on a jquery UI dialog, how can I get allowClear option to work when using a remote datasource? 为什么select2-removal事件不会在带有allowClear的select2中触发? - Why does the select2-removing event not trigger in select2 with allowClear? 如何在Select2下拉菜单上捕获滚动事件? - How to capture scroll event on Select2 dropdown? 如果在select2中选择了第一个选项,如何限制选择 - how to limit selection if first option is chosen in select2 当已经选择了选择选项时发生事件触发器Select2(多个选择选项) - Event Trigger Select2 when the select option is already selected (Multiple Select Option) 如何在select2的所选项目中显示图标? - How to display an icon in the chosen item of select2? 仅当选择了其他选择的选项时,如何显示选择? - How to display a select only when other select's option was chosen? 选择“选择选项”时显示文本。 怎么做? - Displaying text when Select Option is chosen. How to do? 在选择标记中选择一个选项时,如何显示单选框或文本框? - How to display radioboxes or textboxes when an option is chosen in a select tag? 选择特定选择选项时如何调用函数? - How to call a function when a particular select option is chosen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM