【问题标题】:How to prevent unselect event after allowClear has been clicked in select2?在select2中单击allowClear后如何防止取消选择事件?
【发布时间】:2015-05-19 15:53:20
【问题描述】:

我知道对于这个相关问题,unselecting 事件可用于在清除某些选定选项之前触发操作: How to capture event when allowClear option of select2 is chosen?

但它被触发的次数与已选择的选项一样多。如果我清除其中一个选项,它也会被触发。 (见http://jsfiddle.net/6rphh6d3/

我想触发一些动作,但只有当allowClear按钮被点击时,并且只有一次。

到目前为止,我已经考虑过:

  1. 使用此事件并以某种方式过滤它的来源。
  2. 替换AllowClear._handleClear函数的逻辑。

我正在使用 select2 4.0.0。

【问题讨论】:

    标签: jquery jquery-plugins jquery-select2


    【解决方案1】:

    有点晚了,但看到我再次来到这里,我相信这就是你要找的东西。

    var data = [
    {id: 0, text: 'enhancement'},
    {id: 1,text: 'bug'},
    {id: 2,text: 'duplicate'}];
    
    $(".js-example-data-array").select2({
        data: data,
        placeholder: 'placeholder',
        allowClear: true
    }).on("select2:unselecting", function(e)
    {
        $(this).data('state', 'unselected');
                }).on("select2:opening", function(e) {
                    if ($(this).data('state') === 'unselected') {
                        $(this).removeData('state'); 
                        return false;
                    }    
                });
    

    不是最优雅但很有效

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 2021-03-13
      • 2019-08-29
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2015-03-01
      • 2021-03-05
      • 2023-03-16
      相关资源
      最近更新 更多