【问题标题】:Select2 select2-removed event is not affecting other select2Select2 select2-removed 事件不影响其他 select2
【发布时间】:2016-08-19 21:43:57
【问题描述】:

我试图在触发 select2-removed 事件时重置另一个 select2,但我只能影响其他 dom 元素。我想在删除第一个 select2 值时禁用第二个 select2。这是我的代码,¿我做错了吗?我的 select2 版本是 3.5。提前致谢。

 $('#element_select2_1').on("select2-removed", function (e) {
        $('#dom_element').attr('disabled', 'disabled');
        $('#another_dom_element').attr('disabled', 'disabled');

        $('#element_select2_2').select2('disable');
 });

【问题讨论】:

    标签: select2


    【解决方案1】:

    我在任何地方都找不到任何真正的文档“select2-removed”,这是您的活动吗?所以,我创建了一些代码 1. 创建两个 select2,创建一个按钮。在第一个 select2 上注册一个 select2 销毁事件和一个销毁触发该事件的 select2 的按钮。触发的事件禁用第二个 select2。这是你想要做的吗?

                <script>
                    $(function ($) {
                        // first select2
                        $("#sel1").select2();
                        // second select2
                        $("#sel2").select2();
                        // register event to detect select2 being destroyed
                        $("#sel1").next(".select2-container").on("remove", function () { $("#sel2").prop("disabled", true); })
    
                        // button that destroys the select2 triggering the event
                        $("#btn").button().click(function () {
    
                                $("#sel1").select2("destroy");
    
                        });
                    })
                </script>
            </head>
            <body>
                <form>
                    <select tabindex="0" id="sel1" aria-hidden="false">
                        <option value="AQ">Antarctica</option>
                        <option value="AI">Anguilla</option>
                        <option value="AG">Antigua and Barbuda</option>
                        <option value="AR">Argentina</option>
                        <option value="AM">Armenia</option>
                        <option value="AW">Aruba</option>
                    </select><br />
                    <select id="sel2">
                        <option value="BS">Bahamas</option>
                        <option value="BH">Bahrain</option>
                        <option value="BD">Bangladesh</option>
                        <option value="BB">Barbados</option>
                        <option value="BY">Belarus</option>
                        <option value="BE">Belgium</option>
                        <option value="BZ">Belize</option>
                    </select><br />
                    <button id="btn" type="button">remove</button>
    
                </form>
            </body>
    

    【讨论】:

      【解决方案2】:

      假设“select2-removed”按预期触发,请尝试

          $('#element_select2_2').prop('disabled', true);
      

      文档显示了应用于底层选择而不是 select2 控件的属性。

      https://select2.github.io/examples.html#disabled

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        • 2016-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多