为什么 select2 下拉菜单只显示在我的 table 的第一行?

why is the select2 dropdown menu only showing in the first row of my table?

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

这是我的table代码

                    <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();
    })

浏览器中的table

使用class代替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>

然后 变种是管理员; $('.isAdmin').select2({ 占位符:'Choose..', }); $(".isAdmin").on("select2:select select2:unselect", 函数 (e) { //这个 returns 所有 selected 项目 isAdmin = $(this).val(); })