【问题标题】:Sweetalert2 Swal fire disappears after popup. Very strange errorSweetalert2 Swal 火在弹出后消失。很奇怪的错误
【发布时间】:2019-11-20 09:27:16
【问题描述】:

只要成功调用以下函数 1,Sweetalert 对话框就会在打开后立即消失,并且位置 reload() 会起作用。当 number_of_errors > 0 时,代码完全相同,但在后一种情况下,sweetalert 保持打开状态。当成功为 1 时,我如何让它保持开放

注意:由于 ajax 调用,此函数是从另一个 sweetalert 函数调用的。此外,成功/错误图标不显示 :-( 。在 HTML 中很好地引用了 css 和 js 文件

欢迎大家提出意见

提前致谢

function show_comment_result(
    success,
    number_of_errors,
    error_messages,
    action
) {
    if (action == "new item") {
        html_success = "The item was successfully added";
        html_errorA =
            '<p>We had an <font color="#ff0000">error</font>: <p>' +
            '<p><font color="#ff0000">' +
            error_messages +
            '</font></p><b><a href="#" id="temp2" onclick="restart_entry(); ">Please Try Again</a></b>';
        html_errorB =
            '<p> <font color="#ff0000">UNKNOWN error occured</font>: <p><b><a href="#" id="temp2" onclick="restart_entry(); ">Please Try Again</a></b>';
    } else if (action == "edit item") {
        html_success = "The item was successfully edited";
        html_errorA =
            '<p>We had an <font color="#ff0000">error</font>: <p>' +
            '<p><font color="#ff0000">' +
            error_messages +
            "</font></p>";
        html_errorB = '<p> <font color="#ff0000">UNKNOWN error occured</font>';
    } else if (action == "delete item") {
        html_success = "The item was successfully deleted";
        html_errorA =
            '<p>We had an <font color="#ff0000">error</font>: <p>' +
            '<p><font color="#ff0000">' +
            error_messages +
            "</font></p>";
        html_errorB = '<p> <font color="#ff0000">UNKNOWN error occured</font>';
    }

    if (success == 1) {
        Swal.fire({
            type: "success",
            title: "Success!",
            icon: "success",
            showConfirmButton: true,
            html: html_success
        });
        if (action == "edit item" || action == "delete item") {
            location.reload();
        } else if (action == "new item") {
            document.getElementById("HiddenForm").reset();
        }
        //$('#HiddenForm')[0].reset();
        if (action == "new item") {
            document.getElementById("HiddenForm").reset();
        }
    } else {
        if (number_of_errors > 0) {
            Swal.fire({
                icon: "error",
                titleText: "Ooops",
                title: "Ooops...",
                showConfirmButton: false,
                html: html_errorA
            });
        } else {
            Swal.fire({
                icon: "error",
                showConfirmButton: false,
                titleText: "Ooops",
                title: "Ooops...",
                html: html_errorB
            });
        }
    }
}

【问题讨论】:

    标签: javascript sweetalert sweetalert2


    【解决方案1】:

    你可以这样做:

    Swal.fire({
                type: "success",
                title: "Success!",
                icon: "success",
                showConfirmButton: true,
                html: html_success
            }).then(() => {
            if (action == "edit item" || action == "delete item") {
                location.reload();
            } else if (action == "new item") {
                document.getElementById("HiddenForm").reset();
            }
            //$('#HiddenForm')[0].reset();
            if (action == "new item") {
                document.getElementById("HiddenForm").reset();
            }
    })
    

    【讨论】:

    • 谢谢!!!我仍然不明白为什么我的代码不起作用。无论如何……只是一个小补充。我添加了一个匿名函数以与不支持 '=>' 语法的 IE/Edge 兼容。所以 '}).then( function() {' 而不是 '}).then(() => {'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    相关资源
    最近更新 更多