【问题标题】:How can I disable selected attribute from select2() dropdown Jquery?如何从 select2() 下拉 Jquery 中禁用选定属性?
【发布时间】:2014-02-06 20:26:03
【问题描述】:

我知道如何从下拉列表中启用所选属性;我可以使用此代码:

$('select').select2();

但我的问题是如何禁用它?谢谢

【问题讨论】:

  • 它是一个 JQuery 插件

标签: attributes jquery-select2 selected


【解决方案1】:

Select2 3.x 的正确做法是:

$('select').select2("enable", false)

这很好用。

【讨论】:

    【解决方案2】:

    对于使用 Select2 4.x 的用户,您可以通过以下方式禁用单个选项:

    $('select option:selected').prop('disabled', true);
    

    对于使用 Select2 4.x 的用户,您可以使用以下命令禁用整个下拉列表:

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

    【讨论】:

    • 我编辑了我的帖子,请看图片。这种方法不起作用。我认为 select2 有一个特定的功能,但我是新手。
    • 我试过这个版本:$('select option:selected').attr('disabled','true');
    • 我有点困惑,你想禁用选择元素还是选择的选项?如果是选择元素试试:$('select').select2().enable(false);
    • 它第一次工作,但第二次它禁用了所有 2 个下拉菜单。我可以做这样的事情吗? $('#subTasks select').select2().enable(false),其中id="subTasks"属于第二个下拉菜单
    • $('#subTasks').select2().enable(false) => 我想写这个,但是即使我想为Tasks选择另一个值,它也会完全禁用它,Subtask it仍然被禁用。我不知道你是否理解我:D
    【解决方案3】:

    下面的代码也适用于 Select2 3.x

    对于启用选择框:

    $('#foo').select2('enable');
    

    对于禁用选择框:

    $('#foo').select2('disable');
    

    jsfiddle:http://jsfiddle.net/DcunN/

    【讨论】:

    • 禁用:在我的情况下是$('#foo').select2('enable', false);
    • $('#foo').select2('disable');不再起作用
    【解决方案4】:

    要禁用完整的 select2 框,即不删除已选择的值并且不插入新的值,请使用:

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

    其中id-select2 是select2 的唯一ID。如果定义为解决下拉列表,您还可以使用任何特定的类。

    【讨论】:

      【解决方案5】:

      selec2 site 你可以看到选项。 api有“禁用”选项。你可以像这样使用:

      $('#foo').select2({
          disabled: true
      });
      

      【讨论】:

        【解决方案6】:

        从 Select2 4.1 开始,他们已删除对 .enable 的支持

        $("select").prop("disabled", true); // instead of $("select").enable(false);
        

        发件人:https://select2.org/upgrading/migrating-from-35

        【讨论】:

          【解决方案7】:

          由于问题似乎不清楚,如果此答案与原始意图没有直接关系,我很抱歉。

          对于那些使用 Select2 版本 4+ 并根据官方插件文档的用户,.select2("enable") 不再是禁用选择框的方法(不是它的一个选项)。它甚至会从 4.1 版开始完全删除。

          直接从文档中引用(参见https://select2.org/upgrading/migrating-from-35#select2-enable):

          Select2 将尊重底层选择元素的禁用属性。为了启用或禁用 Select2,您应该在元素上调用 .prop('disabled', true/false)。 Select2 4.1 将完全移除对旧方法的支持。

          所以在上一个答案的例子中,它应该是: $('select').prop(disabled,true);

          【讨论】:

            【解决方案8】:

            根据 select2 文档:Click Here

            如果您想禁用 select2,请使用以下方法:

            $(".js-example-disabled").prop("disabled", true);
            

            如果您想启用禁用的 select2 框,请使用以下方法:

            $(".js-example-disabled").prop("disabled", false);
            

            【讨论】:

              【解决方案9】:

              我正在禁用 select2:

              $('select').select2("enable",false);
              

              并启用它

              $('select').select2("enable");
              

              【讨论】:

                【解决方案10】:
                $('select').select2('enable',false);
                

                这对我有用。

                【讨论】:

                  【解决方案11】:

                  我正在禁用价值:

                  <option disabled="disabled">value</option>
                  

                  【讨论】:

                    【解决方案12】:

                    如果你想禁用下拉菜单的值

                    $('select option:not(selected)').prop('disabled', true);

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

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2016-04-13
                      • 1970-01-01
                      • 1970-01-01
                      • 2017-11-06
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多