【问题标题】:Select2 with AJAX appears behind modal带有 AJAX 的 Select2 出现在模态后面
【发布时间】:2016-03-30 13:59:47
【问题描述】:

我在模态中使用 Select2,但它工作不正常,您可以在此处看到:https://gyazo.com/a1f4eb91c7d6d8a3730bfb3ca610cde6

结果出现在模态框后面。我怎样才能解决这个问题?我读过类似的帖子,但都在谈论删除 tabindex,这是我的代码中没有的东西,所以我不知道如何修复它。这是我的代码:

<div class="remodal shadow" data-remodal-id="keuze" data-remodal-options="closeOnOutsideClick: false">
    <button data-remodal-action="close" class="remodal-close"></button>
    <div class="panel-header">Kies uw type logboek</div>
    <div class="modal-body">
        <select id="select" class="searchselectstyle select2"></select>
        <button data-remodal-action="cancel" class="remodal-cancel mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect cancel">Cancel</button>
        <button data-remodal-action="confirm" class="remodal-confirm mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect send">Aanmaken</button>
    </div>
</div>

<script type="text/javascript">
    token = '{{csrf_token()}}';
    $(document).ready(function() {
        $('#select').select2({
            ajax: {
                type: "POST",
                url: "ajax/getlogtypes",
                dataType: 'json',
                data: function (params) {
                    return {
                        q: params.term, // search term
                        page: params.page,
                        '_token' : token
                    };
                },
                escapeMarkup: function (markup) {
                    return markup;
                }, // let our custom formatter work
                minimumInputLength: 1
            }
        });
    });
</script>

【问题讨论】:

  • 检查 DOM。您需要更改select2插件的CSS,使包含选项列表的元素的z-index高于modal的zIndex。

标签: javascript jquery ajax jquery-select2


【解决方案1】:

按照 Rory McCrossan 的建议检查 DOM 后,我发现 Select2 生成的 span 元素的 z-index 较低。我通过在我的代码中添加以下内容来修复它:

.select2-container{
    z-index:100000;
}

【讨论】:

  • 这会产生一些问题,例如出现在导航栏上的字段,如果您使用 2 个模态,则 select2 将出现在当前模态上。
【解决方案2】:

问题与 select2 的父级有关。创建 select2 时,它的父级是 html 正文(而不是模式)。 解决方案是将select2实例化如下:

$('.modal .select2').select2({
    dropdownParent: $('.modal')
});

查看这里了解更多信息:[https://select2.org/troubleshooting/common-problems]

【讨论】:

  • 谢谢。这是最好的答案。我只是在模态中给出了父 div 的 id,它解决了我的问题。 $('.modal .select2').select2({ dropdownParent: $('#divChangeCompanyContainer') });
【解决方案3】:

上述解决方案不适用于我的情况。 但我通过添加下面的 css 解决了它:
.select2-drop {z-index: 99999;}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    相关资源
    最近更新 更多