【问题标题】:having a problem getting the result from a sweetalert2从 sweetalert2 获取结果时遇到问题
【发布时间】:2021-07-09 16:59:19
【问题描述】:
function confirmation() {
    swal.fire({
        title: "Are you sure?",
        text: "Your about to delete some files",
        type: "warning",
        showCancelButton: true,
        showConfirmButton: true,
        confirmButtonText: 'Yes, I am sure!',
        cancelButtonText: "No, cancel it!",
        closeOnConfirm: false,
        closeOnCancel: false
    }).then((result) => {

        if (result.isConfirmed) {
            swal.fire("Deleted!", "files are successfully deleted!", "success");
        } else {
            window.location = "....";
        }
    });
}

但即使我确认或否认它只是取消我能做什么? 我也尝试使用isDenied,但仍然没有。

【问题讨论】:

    标签: javascript php sweetalert2


    【解决方案1】:

    对于 SweetAlert 中的取消按钮,您应该使用 SweetAlertResult 对象的 .isDismissed() 方法。您还使用了已弃用的属性(closeOnConfirm、CloseOnCancel、type):

    function confirmation() {
        swal.fire({
            title: "Are you sure?",
            text: "Your about to delete some files",
            icon: 'success',
            showCancelButton: true,
            showConfirmButton: true,
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
        }).then((result) => {
            if (result.isConfirmed) {
                swal.fire("Deleted!", "files are successfully deleted!", "success");
                console.log("Delete files...")
            }
            if (result.isDismissed) {
                swal.fire("Cancelled!", "Files not deleted!", "warning");
                console.log("Doing Nothing")
            }
        });
    }
    

    JSFiddle:https://jsfiddle.net/sdurwc9y/

    单击取消按钮等于单击模态 SweetAlert 窗口外。所以你可以只使用else 而不是if (result.isDismissed)

    【讨论】:

    • 即使在控制台中我也什么都没有
    • 试用最新版本的 SweetAlert。 JSFiddle 中的 CDN 链接 - jsfiddle.net/sdurwc9y
    猜你喜欢
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2014-08-11
    相关资源
    最近更新 更多