简体   繁体   English

Bootstrap Modal 在通过 Javascript 触发时不会向正文添加模态开放类

[英]Bootstrap Modal not adding modal-open class to body when triggered via Javascript

I have a Modal on my page whose content changes depending on which of two buttons is clicked (an <input /> 's value changes).我的页面上有一个 Modal,其内容会根据单击两个按钮中的哪一个而发生变化( <input />的值发生变化)。 To better add functionality, I have set up an additional block of that is supposed to manual open the Modal if there's an error to show.为了更好地添加功能,我设置了一个额外的块,如果有错误显示,应该手动打开模态。

To make sure the form is "sticky", I manually trigger the click event on the corresponding button and what not.为了确保表单是“粘性的”,我手动触发了相应按钮上的click事件,其他什么不是。 However, when I manually trigger the click event, Bootstrap is not adding .modal-open to the body.但是,当我手动触发click事件时,Bootstrap 没有将.modal-open添加到正文中。 Due to how my site is built, without .modal-open on the body, the Modal is completely hidden behind other content.由于我的网站的构建方式,主体上没有.modal-open ,Modal 完全隐藏在其他内容之后。

So obviously I'm doing something wrong, but I have no idea.所以很明显我做错了什么,但我不知道。 Here's a copy of my code:这是我的代码的副本:

<?php
$modalerror = false;
$id = 0;
if (/*Basic check for errors*/) {
    $modalerror = true;
    $id = /*Get the correct id*/;
}
?>
<script type="text/javascript">
    jQuery( document ).ready(function($) {
        $('#modal').on('show.bs.modal', function(event) {
            var button = $(event.relatedTarget);

            if (button.attr('data-name') && button.attr('data-name') != '') {
                $('#name').val(button.attr('data-name'));
            } else {
                $('#name').val('');
            }
        });
    });

    <?php if ($modalerror) { ?>
        jQuery ( window ).load(function() {
            jQuery('button[data-id=<?php echo $id; ?>]').trigger('click');
        });
    <?php } ?>
</script>

Upon further inspection/progress, I noticed that it I manually trigger the Modal in the exact same fashion somewhere else on the page/later on, there aren't any problems with adding the .modal-open class to the body.在进一步检查/进展后,我注意到我以完全相同的方式在页面上的其他地方手动触发模态/稍后,将.modal-open类添加到.modal-open没有任何问题。 Code sample:代码示例:

<script type="text/javascript">
    function manualOpen(id) {
        jQuery('button[data-id="'+id+'"]').trigger('click');
    }
</script>
<button type="button" onclick="manualOpen(/*Correct id*/);">Open</button>

Even more testing, I added a second modal to the page with completely different contents that opens automatically under very specific circumstances, it does not need to load custom content based off of a buttons data-* attributes.甚至更多的测试,我向页面添加了第二个模式,其中包含在非常特定的情况下自动打开的完全不同的内容,它不需要根据按钮data-*属性加载自定义内容。 This second Modal suffers the exact same problem if it doesn't also have some form of timeout (as mentioned in my comment to this question).如果第二个 Modal 也没有某种形式的超时(如我对此问题的评论中所述),则它会遇到完全相同的问题。

Using bootstrap 4, the best way IMO without using timeouts is adding a function in the closing event of the modal.使用引导程序 4,IMO 不使用超时的最佳方法是在模态的关闭事件中添加一个函数。

When a modal is closing and there is a command for open another modal before it completes the closing action, the new modal is open but the class .modal-open is not added to the body.当一个模态正在关闭并且在完成关闭操作之前有一个打开另一个模态的命令时,新的模态是打开的,但类 .modal-open 不会添加到主体中。

The next function open a modal checking if there is an opened modal.下一个函数打开一个模态,检查是否有一个打开的模态。 The new modal is opened directly or in the closing event callback depending of the case.根据情况直接打开新的模态或在关闭事件回调中打开。 This function requires that every modal has an id.此功能要求每个模态都有一个 id。

function openModal(modalid) {
    //Looks for current modal open
    if ($('.modal.fade.show').length > 0) { 

        //Gets the id of the current opened modal
        var currentOpenModalId = $('.modal.fade.show').attr('id');

        //Attaches a function to the closing event
        $('#' + currentOpenModalId).on('hidden.bs.modal', function () {

            //Opens the new model when the closing completes
            $('#' + modalid).modal('show');

            //Unbinds the callback
            $('#' + currentOpenModalId).off('hidden.bs.modal');
        });

        //Hides the current modal
        $('#' + currentOpenModalId).modal('hide');
    } else {
        //If is not an opened modal, the new modal is opened directly
        $('#' + modalid).modal('show');
    }
}

I am using Bootstrap 4.1.1 and jQuery 3.3.1, and faced the same problem.我正在使用 Bootstrap 4.1.1 和 jQuery 3.3.1,并且遇到了同样的问题。 50ms didn't solved the problem. 50ms 没有解决问题。 So, i had to bump it upto 500ms, and it worked!所以,我不得不把它提高到 500 毫秒,它奏效了!

//hide first
$('#modal1').modal('hide');
//add delay when showing next modal
setTimeout(function () {
  $('#modal2').modal('show');
}, 500);

I'm using bootstap 4.3.1 and modified 'bootstrap.min.js' file我正在使用 bootstap 4.3.1 并修改了“bootstrap.min.js”文件

//bootstrap.min.js (line : 1646)
this._showBackdrop(function () {
    g(document.body).removeClass(ce); // ce = 'modal-open'
    t._resetAdjustments(),
    t._resetScrollbar(),
...

to

//bootstrap.min.js (line : 1646)
this._showBackdrop(function () {
    if (document.getElementsByClassName('modal fade show').length == 0) {
        g(document.body).removeClass(ce);
    }
    t._resetAdjustments(),
    t._resetScrollbar(),
...

It works very well它运作良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM