javascript - 无法阻止甜蜜警报关闭

标签 javascript jquery sweetalert2

我试图在关闭 SweetAlert 之前验证 email 字段,但由于某种原因 SweetAlert 在发现错误后关闭:

Swal.fire({
    title: title,
    html: template,
    preConfirm: function (email) {
        email = '';
        return new Promise(function (resolve, reject) {
            setTimeout(function () {
                if (email === '') {
                    alert("err");
                    reject('err')
                } else {
                    resolve()
                }
            }, 1000)
        }).catch(err => alert("error catched"))
    },
});

如何防止 SweetAlert 模式在捕获到错误时关闭?

谢谢

最佳答案

这是一个功能齐全的示例(用于解决您的特定问题):

<html>
    <body>
        <div class="showDialogButton">Show dialog</div>

        <script src="https://code.jquery.com/jquery-3.4.1.js"
            integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
            crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
        <script type="text/javascript">

            $(() => {

                const title = "Hi"
                const template = `<b>hello</b> world`

                $(".showDialogButton").click(() => {
                    Swal.fire({
                        title: title,
                        html: template,
                        preConfirm: function (email) {
                            email = '';
                            return new Promise(function (resolve, reject) {
                                setTimeout(function () {
                                    if (email === '') {

                                        reject('Email is empty!')

                                    } else {

                                        resolve()
                                    }
                                }, 1000)
                            }).catch(err => {
                                alert(`error: ${err}`)
                                return false
                            })
                        },
                    });
                })
            })
        </script>
    </body>
</html>

默认行为是关闭对话框。为了覆盖它,您必须从传递给 .catch 的函数中返回 false

来自 https://sweetalert2.github.io/#configuration

Argument: preConfirm

Default value: null

Description: Function to execute before confirm, may be async (Promise-returning) or sync. Returned (or resolved) value can be:

  • false to prevent a popup from closing
  • anything else to pass that value as the result.value of Swal.fire()
  • undefined to keep the default result.value

关于javascript - 无法阻止甜蜜警报关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57842624/

相关文章:

javascript - Google map + 街景问题 - 如何禁用 Photo Sphere

javascript - 隐藏 Div JQuery 后无法显示

javascript - jsRender 和 jsViews 之间的区别

javascript - 如何在关闭第一个 UI Datepicker 时打开第二个 UI Datepicker

javascript - 选择示例中来自文件的 sweetalert2 inputoptions

javascript - 提示关闭后 CSS 消失

javascript - 如何从按钮中删除类并在 sweetalert2 中添加自定义类?

javascript - servlet 不通过 angularJS 发送响应

javascript - Google AppEngine 会支持 Javascript 吗?

javascript - 如何隐藏某些 child ()。一个 parent ()。分区?