【问题标题】:Show a validation message in "then" instead of closing the Swal automatically在“then”中显示验证消息,而不是自动关闭 Swal
【发布时间】:2020-04-22 01:07:36
【问题描述】:

preConfirm 获取之后,then 方法中是否有可能没有关闭 Sweetalert (2)?如果请求返回特定的内容,我只想让 Swal 保持打开状态,但执行 Swal.showValidationMessage() 而不是打开一个全新的警报。在then 方法中执行验证消息时,它会显示片刻,但警报会自动关闭。

有没有办法让警报保持打开状态?

【问题讨论】:

    标签: sweetalert2


    【解决方案1】:

    如果我没记错的话,您应该在preConfirm() 选项中调用Swal.showValidationMessage() 函数,而不是在then() 承诺返回中。

    验证您在 preConfirm() 中查找的任何输入是否可以正常工作?

    例子:

    Swal.fire({
        title: "title",
        text: "text",
        icon: "info",
        showCancelButton: true,
        cancelButtonText: "Cancel",
        confirmButtonText: "Done!",
        html: `<input id="input1 class="swal2-input" />
               <input id="input2 class="swal2-input" />`,
        preConfirm: () => {
            let value1 = Swal.getPopup().querySelector('#input1').value;
            let value2 = Swal.getPopup().querySelector('#input2').value;
    
            if (value1 == somethingWrong1) {
                // Notify error and keep alert open.
                Swal.showValidationMessage('Invalid input #1.');
    
            } else if (value2 == somethingWrong2) {
                // Notify error and keep alert open.
                Swal.showValidationMessage('Invalid input #2.');
    
            } else {
                // Promise return value that will be available inside the 'then()'.
                // Alert will now close.
                return {
                    input1: input1,
                    input2: input2
                };
            }
        }
    }).then((inputs) => {
        if (inputs !== "cancel") {
            // Access the inputs using: inputs.value.input1 and inputs.value.input2
            // And do whatever you want.
        }
    });
    

    【讨论】:

    • 问题是我需要验证请求的返回,我似乎无法访问 preConfirm 中返回的数据 - 只有“then”方法
    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 2013-09-05
    • 1970-01-01
    • 2021-02-24
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多