简体   繁体   English

Bootstrap V5 警报解除:单击“关闭按钮”时要求确认

[英]Bootstrap V5 Alert Dismissing: Asking for confirmation when “Close Button” is clicked

I'm trying to add a nice confirmation message using Sweetalert2 to the bootstrap version 5 alert component.我正在尝试使用 Sweetalert2 向引导程序版本 5 警报组件添加一条很好的确认消息。

how does it work?它是如何工作的?

when I clicked to "Close Button" (X) it should display a confirmation message with two options (Yes) and (No) if I clicked to (Yes) the alert component should be closed if (No) the alert component should be displayed.当我点击“关闭按钮”(X)时,它应该显示一个带有两个选项(是)和(否)的确认消息,如果我点击(是)警报组件应该关闭,如果(否)应该显示警报组件.

that's it.就是这样。

the issue that I face is in the close event when I put this code inside it and clicked to (Yes) Option in sweatalert2 confirmation message it shows me an error message in my dev console!当我将此代码放入其中并单击“是”选项时,我面临的问题是 close 事件中的sweatalert2 确认消息,它在我的开发控制台中向我显示了一条错误消息!

reference:https://getbootstrap.com/docs/5.0/components/alerts/#dismissing参考:https ://getbootstrap.com/docs/5.0/components/alerts/#dismissing

HTML code: HTML代码:

<div class="alert alert-warning alert-dismissible fade show" role="alert">
  <strong>Holy guacamole!</strong> You should check in on some of those fields below.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

JS code: JS代码:

if ($(".alert").length) {
    $(".alert").on("close.bs.alert", function (e) {
        e.preventDefault();
        var swalWithBootstrapButtons = Swal.mixin({
            customClass: {
                confirmButton: "btn btn-primary",
                cancelButton: "btn btn-danger me-3",
            },
            buttonsStyling: false,
        });
        swalWithBootstrapButtons
            .fire({
                title: "Are you sure?",
                text: "You won't be able to revert this!",
                icon: "warning",
                confirmButtonText: "<i class='fas fa-check-circle me-1'></i> Yes, I am!",
                cancelButtonText: "<i class='fas fa-times-circle me-1'></i> No, I'm Not",
                showCancelButton: true,
                reverseButtons: true,
                focusConfirm: false,
            })
            .then((result) => {
                if (result.isConfirmed) {
                    var alertNode = document.querySelector(".alert");
                    var alert = bootstrap.Alert.getInstance(alertNode);
                    alert.close();
                }
            });
    });
}

Issues问题

  1. alert instance was not created .创建alert实例。 so create it before closing the alert.所以在关闭警报之前创建它。

  2. off the close.bs.alert event before closing the alert programmatically.在以编程方式关闭警报之前关闭close.bs.alert事件。 It prevents infinite execution.它可以防止无限执行。

Note笔记

You can also use jQuery with bootstrap 5. So it can also be simplified as你也可以在 bootstrap 5 中使用 jQuery。所以它也可以简化为

$(e.target).off('close.bs.alert').alert('close'); 

 $(".alert").on("close.bs.alert", function(e) { e.preventDefault(); var swalWithBootstrapButtons = Swal.mixin({ customClass: { confirmButton: "btn btn-primary", cancelButton: "btn btn-danger me-3", }, buttonsStyling: false, }); swalWithBootstrapButtons .fire({ title: "Are you sure?", text: "You won't be able to revert this!", icon: "warning", confirmButtonText: "<i class='fas fa-check-circle me-1'></i> Yes, I am!", cancelButtonText: "<i class='fas fa-times-circle me-1'></i> No, I'm Not", showCancelButton: true, reverseButtons: true, focusConfirm: false, }) .then((result) => { if (result.isConfirmed) { // using js var alertNode = document.querySelector(".alert"); var alert = new bootstrap.Alert(alertNode); $(alertNode).off('close.bs.alert') alert.close(); // using jQuery // $(e.target).off('close.bs.alert').alert('close'); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script> <div class="alert alert-warning alert-dismissible fade show mt-5" role="alert"> <strong>Holy guacamole!</strong> You should check in on some of those fields below. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>

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

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