【问题标题】:why is the select2 dropdown menu only showing in the first row of my table?为什么 select2 下拉菜单只显示在我的表格的第一行?
【发布时间】:2020-12-20 12:36:00
【问题描述】:

大家好,我正在尝试使用 select2 制作下拉菜单。我的 select2 下拉菜单遇到问题。出于某种原因,select2 下拉菜单仅显示在我的表格的第一行。但我不明白为什么会这样。我希望 select2 下拉菜单显示在整个管理列中,而不仅仅是第一行。

这是我的表格代码

                    <tbody id="myTable">
                        <tr class="table">
                            <td class="table">email</td>
                            <td class="table">name</td>
                            <td class="table">
                                <form>
                                    <select id="isAdmin">
                                        <option selected>false</option>
                                        <option value="false">false</option>
                                        <option value="true">true</option>
                                    </select>
                                </form>
                            </td>
                            <td class="table">bhv</td>
                        </tr>
                    </tbody>

这是我的 select2 代码

    var isAdmin;
    $('#isAdmin').select2({
        placeholder: 'Choose..',
    });
    $("#isAdmin").on("select2:select select2:unselect", function (e) {
        //this returns all the selected item
        isAdmin = $(this).val();
    })

这是浏览器中的表格

【问题讨论】:

    标签: html jquery-select2


    【解决方案1】:

    使用类代替Id,

    一个页面上不能有 2 个或多个具有相同 ID 值的项目。

                    <tbody id="myTable">
                        <tr class="table">
                            <td class="table">email</td>
                            <td class="table">name</td>
                            <td class="table">
                                <form>
                                    <select class="isAdmin">
                                        <option selected>false</option>
                                        <option value="false">false</option>
                                        <option value="true">true</option>
                                    </select>
                                </form>
                            </td>
                            <td class="table">bhv</td>
                        </tr>
                    </tbody>
    

    然后 var isAdmin; $('.isAdmin').select2({ 占位符:'选择..', }); $(".isAdmin").on("select2:select select2:unselect", function (e) { //这将返回所有选定的项目 isAdmin = $(this).val(); })

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2021-12-21
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2017-06-09
      相关资源
      最近更新 更多