在“然后”中显示验证消息,而不是自动关闭Swal

问题描述 投票:0回答:1

then提取后,是否可能在preConfirm方法中不关闭Sweetalert (2)?如果请求返回特定的内容,我想简单地保持Swal为打开状态,但是执行Swal.showValidationMessage()而不是打开整个新警报。在then方法中执行验证消息时,会短暂显示该消息,但警报会自动关闭。

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

sweetalert2
1个回答
0
投票

[如果我没记错的话,您应该在Swal.showValidationMessage()选项内而不是在preConfirm()许诺返回中调用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.
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.