从sweetalert2获取结果时遇到问题

57hvy0tb  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(360)
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 还是什么都没有。

bcs8qyzn

bcs8qyzn1#

对于sweetalert中的取消按钮,您应该使用 .isDismissed() sweeetalertresult对象的方法。您还使用了不推荐使用的属性(CloseOnConfig、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/
单击“取消”按钮等同于在警报窗口外单击。所以你可以用 else 而不是 if (result.isDismissed) .

相关问题