简体   繁体   English

Bootstrap 关闭模式不起作用

[英]Bootstrap close modal not working

I have two button here that are being used to close the modal.我这里有两个按钮用于关闭模式。 The first is the close icon and the other one is cancel button, both use data-dismiss to close the modal.第一个是关闭图标,另一个是取消按钮,都使用 data-dismiss 来关闭模态。 However, both of them are not working.但是,它们都不起作用。 I am using the same code for another modal and there they are working fine.我对另一个模式使用相同的代码,它们工作正常。 Any guesses?有什么猜测吗?

<div id="timeSelectModal{{entry.position - 1}}" style="display: none" class="modal">
    <div class="modal-dialog">
        <div id="timeSelectModalContent" class="modal-content">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <label>
                <input id="checkbox8pm{{entry.position - 1}}" type="checkbox" value="first_checkbox">
                <label class="checkbox-label">Thursday, 08:00 pm.</label>
            </label>
            <br>
            <label>
                <input id="checkbox9pm{{entry.position - 1}}" type="checkbox" value="second_checkbox">
                <label class="checkbox-label">Thursday, 09:30 pm.</label>
            </label>
            <div id="time-modal-footer" class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-success" id="timeSaveButton{{entry.position - 1}}" v-on:click="setTime(entry.position - 1)">Save</button>
            </div>
        </div>
    </div>
</div>

remove the "fade" class.删除“淡入淡出”类。

 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

change to改成

<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

Fade is an effect, if you remove fade, your modal will not have fade effect.淡入淡出是一种效果,如果您去除淡入淡出,您的模态将不会有淡入淡出效果。 The several problem which cause this issue is not call modal with right method.导致此问题的几个问题是没有使用正确的方法调用模态。

Wrong method : $("#myModal").show();错误的方法: $("#myModal").show();

Right method : $("#myModal").modal('show');正确的方法: $("#myModal").modal('show');

Very old topic but it still shows up first for people searching this problem, likely caused by them making the same simple mistake I did which was failing to ensure that the modal div was outside of the main body div from which it was launched.很老的话题,但它仍然首先出现在搜索这个问题的人身上,这可能是因为他们犯了和我一样的简单错误,未能确保模态 div 位于启动它的主体 div 之外。 To use the Material Kit as an example, you should double check that the target "#myModal" div is outside the closing div tag for your main container.以 Material Kit 为例,您应该仔细检查目标“#myModal”div 是否在主容器的结束 div 标签之外。

<div class="main main-raised">
    <div class="container">
    <!--                 modals -->
        <div class="row" id="modals">
            <div class="col-md-6">
                <button class="btn btn-primary btn-raised btn-round" data-toggle="modal" data-target="#myModal">
                    Classic modal
                </button>
            </div>

        </div>
    <!--                 end modals              -->
    </div>
</div>

<!-- Classic Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    <i class="material-icons">clear</i>
                </button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                <p>Hey hey</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-simple">Nice Button</button>
                <button type="button" class="btn btn-danger btn-simple" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<!--  End Modal -->

First check you have included bootstrap.js file in your html correctly.首先检查您是否在 html 中正确包含了 bootstrap.js 文件。

You can try this code and replace the button tag for closing modal by -您可以尝试使用此代码并通过以下方式替换用于关闭模式的按钮标记 -

<a href="#" class="close" data-dismiss="modal" aria-label="close">&times;</a>

If still doesn't work.. Let me know.!如果仍然不起作用..让我知道。!

You can add an on-click event on close button in jQuery.您可以在 jQuery 中的关闭按钮上添加点击事件。 Like this-像这样-

$("#yourModal").modal("hide"); 

I was stuck on this for a while also.我也被困在这个问题上一段时间。 I have no idea why but when I used data-bs-dismiss in stead of data-dismiss in the class for the close button it worked for me.我不知道为什么,但是当我在类中使用data-bs-dismiss而不是data-dismiss来关闭按钮时,它对我有用。

Please see below full code for close button.请参阅以下关闭按钮的完整代码。

Close

在我的情况下有什么帮助,我已经用 data-bs-dismiss 替换了 data-dismiss,现在它可以工作了!

Here is my implementation of a modal you can use for comparing to help troubleshoot what errors exist in your modal code.这是我对模态的实现,您可以使用它进行比较,以帮助解决模态代码中存在的错误。

<html lang="en">
<head>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<h2>click Here</h2>
<div class="container">
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

</div>

</body>
</html>

Give ID 'modalClose' to Model's Close button and ID 'myView' to your Model,为模型的关闭按钮提供 ID 'modalClose',为您的模型提供 ID 'myView',

then use the following code:然后使用以下代码:

    <script>
    $(function () {
        $('#modalClose').on('click', function () {
            $('#myView').hide();
        })
    })

</script>

It will work like a charm.它会像魅力一样发挥作用。

I had this issue too and found this page so thought I would post my solution for anybody who may do the same.我也有这个问题并找到了这个页面,所以我想我会为任何可能做同样事情的人发布我的解决方案。

Mine was down to the way the modal appeared.我的归结为模态出现的方式。 Somebody had use .toggle() when it should be .modal('show') so I guess from the modal point of view it never appeared properly so it can't disappear either.有人在应该是 .modal('show') 时使用了 .toggle() 所以我猜从模态的角度来看它从来没有正确出现所以它也不会消失。

Hope that helps somebody living in the future.希望对未来生活的人有所帮助。

it happens when you use bootstrap 5, the below code worked for me.当您使用 bootstrap 5 时会发生这种情况,下面的代码对我有用。 I hope it will work for you.我希望它对你有用。

<button type="button" class="close" data-bs-dismiss="modal">X</button>

Thanks谢谢

始终尝试将模态的 HTML 代码放在文档中的顶级位置,以避免其他组件影响模态的外观和/或功能。

In my case I had other div over it.就我而言,我有其他 div。
I'v solved with z-index.我用 z-index 解决了。

.modal button.close {
    z-index: 1000;
}

For me modal was not getting closed because script files had conflict somewere, so used minimum required script files to check the problem in closing model.Way I added scripts for which model was getting closed properly对我来说,模态没有被关闭,因为脚本文件存在冲突,所以使用最少的脚本文件来检查关闭模型时的问题。我添加了正确关闭模型的脚本

"scripts": [
          "src/assets/js/lib/jquery/jquery.min.js",
          "src/assets/js/lib/bootstrap/js/popper.min.js",
          "src/assets/js/lib/bootstrap/js/bootstrap.min.js"
        ]

And for sure I had added bootstrap.min.css in styles.当然我已经在样式中添加了bootstrap.min.css

In my case I was missing the data-target attribute on the link that calls the modal.在我的情况下,我缺少调用模态的链接上的data-target属性。

<a href="#getResults"
   class="results-link"
   data-toggle="modal"
   data-target="#results-modal"
   data-providerName="${providerName}"
   data-individualID="${individualID}"
   data-reportID="@Model.ReportID">Results
</a>

I had the same issue.我遇到过同样的问题。 I fixed the problem adding "events.js" where inside i wrote this code:我解决了添加“events.js”的问题,我在里面写了这段代码:

$("button[data-dismiss=modal]").click(function()
{
  $(".modal").modal('hide');
});

By adding this code in each head of your page u will able to close modal by native buttons.通过在页面的每个头部添加此代码,您将能够通过本机按钮关闭模式。 I hope this solution can ben helpful for someone我希望这个解决方案对某人有帮助

None of the answers helped me.没有一个答案对我有帮助。 But I was having issues as well.但我也遇到了问题。

I forgot to add the JavaScript file of Bootstrap.我忘记添加 Bootstrap 的 JavaScript 文件了。 What I added was just the CSS of Bootstrap.我添加的只是 Bootstrap 的 CSS。 Both the Bootstrap CSS and JS CDN links need to be added to the page. Bootstrap CSS 和 JS CDN 链接都需要添加到页面中。

So make sure you add this Bootstrap CDN to add the script:因此,请确保添加此 Bootstrap CDN 以添加脚本:

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>

SUMMARY OF BOOTSTRAP 5 ISSUE引导程序 5 问题摘要

Hi.你好。 Like everyone else this tripped me up.像其他人一样,这让我感到沮丧。 This thread has spanned a long period and certainly there have been other issues causing the problem.这个线程已经跨越了很长一段时间,当然还有其他问题导致了这个问题。 However, as Alexandre Elshobokshy has mentioned unwittingly, changing然而,正如Alexandre Elshobokshy无意中提到的,改变

data-bs-dismiss

instead of代替

data-dismiss

works.作品。 I wanted to call this out since it is correct but there are additional keys that need to be updated for Modals to work with Bootstrap and HTML 5.我想指出这一点,因为它是正确的,但还有一些额外的键需要更新,Modals 才能与 Bootstrap 和 HTML 5 一起使用。

HTML5 has changed the tags required by Bootstrap. HTML5 更改了 Bootstrap 所需的标签。 One is the data-bs-dismiss.一种是data-bs-dismiss。

The others are :其他的是

data-bs-toggle & data-bs-target

I hope this helps others !我希望这对其他人有帮助!

if you put into your code (in some cases it makes sense):如果你输入你的代码(在某些情况下它是有道理的):

$modal.off('click')

then it also results that the modal won't be closed because the event is blocked.那么它也会导致模态不会因为事件被阻止而关闭。 You can test it whether you can close the modal by pressing ESC.您可以按ESC测试是否可以关闭模​​态。 It that works then your click events are blocked.它有效然后您的点击事件被阻止。

You can debug it like this where you will see that it returns false:你可以像这样调试它,你会看到它返回false:

$modal.on('click',function(event){
    console.log('debug click');
    console.log(event);
});

In that case you have to handle it yourself:在这种情况下,您必须自己处理:

$modal
  .on('click', '[data-dismiss="modal"]', function() {
      $modal.modal('hide');
  });

 const backdrops = document.querySelectorAll('.modal-backdrop.fade.show'); Array.prototype.forEach.call(backdrops, function (node) { node.parentNode.removeChild(node); }); $(`#${data.uniqueId}`).modal('hide'); $(`#${data.uniqueId}`).parent().removeClass("open");
 body { padding-right: 0;important: overflow; auto !important; }

I ran into this issue recently, all modals in my app acted correctly, but new one was 'mibehaving'.我最近遇到了这个问题,我的应用程序中的所有模式都正确运行,但新的模式是'mibehave'。 Anyway long story short, dont confuse总之长话短说,不要混淆

$("#flash-modal").modal('show');

with

$("#flash-modal").show();

show() is jQuery method changing css atribute display, you need to init it correctly with modal . show() 是 jQuery 方法更改 css 属性显示,您需要使用modal正确初始化它。 Hope this helps someone...希望这可以帮助某人...

i was calling jquery on target page.我在目标页面上调用 jquery。 I noticed that when I removed this line, the close command worked.我注意到当我删除这一行时,关闭命令起作用了。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

then I called the old version of jquery and realized that it was working again.然后我打电话给旧版本的 jquery 并意识到它又可以工作了。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

I don't know why it prevents the close button from working on 3.x and above..我不知道为什么它会阻止关闭按钮在 3.x 及更高版本上工作..

in 2023 use 2023年使用

$('#mypopup').modal('show');

instead of代替

$('#mypopup').show();

either rises the popup w/o any issue.要么在没有任何问题的情况下弹出弹出窗口。 But that Cancel works with first one only但是取消只适用于第一个

Have a look and ensure that you are not including the html data attributes to toggle the modal if you are handling the logic in javascript because this will cause the event to double fire, creating two modal-backdrop divs.如果您正在处理 javascript 中的逻辑,请查看并确保您没有包含 html 数据属性来切换模态,因为这会导致事件双重触发,从而创建两个模态背景 div。 This means that on hide/dismiss, it will remove 1 modal-backdrop but leaves the duplicate and hence it appears not to have closed.这意味着在隐藏/关闭时,它将删除 1 个模态背景但保留副本,因此它似乎没有关闭。

On the event that presents the modal, have your dev tools open and look at the DOM, you will clearly see two.modal-backdrop divs are spawned.在呈现模态的事件中,打开您的开发工具并查看 DOM,您会清楚地看到生成了两个 modal-backdrop div。

Solution in this case is to choose between html data-attributes and JS logic, but not both.这种情况下的解决方案是在 html 数据属性和 JS 逻辑之间进行选择,但不能同时选择两者。

use this code close model使用此代码关闭模型

 <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> </head> <body> <h2>click on button</h2> <div class="container"> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <p>Some text in the modal.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </body> </html>

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

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