【问题标题】:select2 will not disable or clearselect2 不会禁用或清除
【发布时间】:2013-07-14 21:27:12
【问题描述】:

我有一个次要选择从属于主要选择(选择一家商店,然后选择一个部门 - 与选择一个国家,然后选择一个州相同)。

无论我撕掉多少其他代码,我都绝对无法让 select2 的 ('enable',false) 和 ('data', null) 方法工作。

<select id="stores">
  <option value="foo">foo</option>
</select>
<select id="depts">
  <option value="bar">bar</option>
</select>

// ...some logic that selects a store, then fetches that store's depts ...

$('#depts').select2('enable',false);// does not disable control
$('#depts').select2('data',null); // does not clear control

所以我不得不这样做:

$('select#depts').empty(); // clears HTML element
$('#depts').select2(); // re-enhances select w/ select2
$('#depts').select2('disable',true); // disables 

它在 jsfiddle 中表现自己,所以我什至无法发布示例并请求帮助。我只是……难住了。

【问题讨论】:

    标签: jquery-select2


    【解决方案1】:
    // to disable
    $('#statusSelect').select2('disable');
    
    //to enable
    $('#statusSelect').select2('enable');
    

    【讨论】:

      【解决方案2】:

      这适用于所有浏览器:

      $('#statusSelect').select2('destroy');
      $('#statusSelect').prop('disabled', true);
      $('#statusSelect').select2();
      

      这会给你一个禁用的 select2 输入。

      简单地重新启用:

      $('#statusSelect').select2('destroy');
      $('#statusSelect').prop('disabled', false);
      $('#statusSelect').select2();
      

      不利的一面是,当 select2 被销毁并重新应用时,这会导致您的选择框在瞬间改变外观。

      此外,旧版本的 select2 不支持“只读”和“禁用”。

      【讨论】:

      【解决方案3】:

      您可能遇到了 select2 错误 1104。不幸的是,在 IE8-10 中它仍然是一个问题,但这不是 select2 的错。问题是当元素被禁用时 IE 不会触发 propertychange 事件,也没有实现其他浏览器使用的 MutationObserver 功能。幸运的是,我编写了一个小型 jQuery 插件,它允许在禁用元素时触发 propertychange 事件。你可以找到它here

      $('#originalSelect').fireOnDisable().select2();
      $('#originalSelect').prop('disabled', true);
      

      现在应该可以在 IE8-10 中使用。如果你需要支持 IE6-7,你就靠自己了!

      【讨论】:

      • 在 IE9/IE10 中,禁用属性对多选 2 不起作用。
      • IE8-10 没有任何 attribute 观察器,只有属性观察器 - 因此您可能会遇到设置属性而不是属性的问题 - 在我的测试使用jquery到.prop()可以正常工作,但不是.attr()
      【解决方案4】:

      如果您有 select2 下拉框的唯一 ID,请使用该 ID,

      $("#id-select2").attr("disabled", true);
      

      【讨论】:

        【解决方案5】:

        此代码应适用于所有浏览器(select2 v4.0.4):

        禁用:

        $('select').prop('disabled', true).trigger('change');
        

        启用:

        $('select').prop('disabled', false).trigger('change');
        

        【讨论】:

          【解决方案6】:

          对于 IE,您必须在启用/禁用后重新初始化 select2

          // change action disable
          $('.custom-select').select2()
          .on("change", function(e) {
             $('.custom-select').prop("disabled", true).select2(); //call select2
          })
          
          // disable alternative
           $('.custom-select').prop("disabled", true).select2(); //call select2
          

          【讨论】:

            【解决方案7】:

            你不需要这个吗:

            $('#depts').prop('disabled', false);

            this question

            【讨论】:

            • 这个问题和你链接的问题不一样,因为它使用了select2插件,它不是一个简单的选择被禁用。
            【解决方案8】:

            如果有人尝试在 .net 服务器代码中执行此操作:

            this.mySelect2elementID.Attributes.Add("disabled", "true");
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-11-24
              • 1970-01-01
              • 2022-11-28
              • 2020-01-08
              • 1970-01-01
              • 1970-01-01
              • 2020-04-07
              • 2018-07-02
              相关资源
              最近更新 更多