简体   繁体   English

无法在自定义 HTML 按钮单击时关闭 Sweet Alert 2 模式

[英]Cannot Close Sweet Alert 2 Modal On Custom HTML Button Click

I am having a very hard time for debugging this.我很难调试这个。 I am using custom HTML parameter from SweetAlert2 .我正在使用来自SweetAlert2 的自定义 HTML 参数。 What I want to achieve is that whenever I click "Back" Button, I want to close the modal.我想要实现的是,每当我单击“返回”按钮时,我都想关闭模态。 I did read the documentation.我确实阅读了文档。 we can usee swal.close() or swal.closeModal().我们可以使用 swal.close() 或 swal.closeModal()。 But when I doing it, I can't close the modal upon a back button.但是当我这样做时,我无法在后退按钮上关闭模态。 Below are the codes.下面是代码。

//declare custom html
        const cancelBtn      = `<button class="cancelSwalBtn" id="standardCancelBtn" >Back</button>`;
        const removeAddOnBtn = `<button class="removeAddonSwalBtn" id="standardRemoveAddonBtn">Remove Add-ons</button>`;
        const proceed        = `<button type="button" role="button" tabindex="0" class="proceedSwalBtn" id="standardProceedBtn">Proceed</button>`;
        const html           = `<p>Your voucher does not cover the cost additional of addons. </p><div class="btn-holder">${cancelBtn}${removeAddOnBtn}${proceed}</div>`;

        //custom swal
        swal({
            type: 'info',
            title: 'Info',
            html: `${html}`,
            width :700,
            showCancelButton: false,
            showConfirmButton:false,
            onOpen: (swal) => {
                //close btn
               $(swal).find('#standardCancelBtn').click(function (e) {
                   console.log('in');
                   swal.close();
                   //swal.closeModal();
               })
            }

        }).then((result) =>{
            console.log(result);
        });

swal.close() or swal.closeModal() should work. swal.close()swal.closeModal()应该可以工作。

Ref.: SweetAlter Methods参考: SweetAlter 方法

Simply add: onclick="swal.closeModal(); return false;"只需添加: onclick="swal.closeModal(); return false;" inside your html button like this:在您的 html 按钮中,如下所示:

const cancelBtn = `<button class="cancelSwalBtn" id="standardCancelBtn" onclick="swal.closeModal(); return false;">Back</button>`;
onclick="swal.close(); return false;"

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

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