【发布时间】:2020-04-22 01:07:36
【问题描述】:
在preConfirm 获取之后,then 方法中是否有可能没有关闭 Sweetalert (2)?如果请求返回特定的内容,我只想让 Swal 保持打开状态,但执行 Swal.showValidationMessage() 而不是打开一个全新的警报。在then 方法中执行验证消息时,它会显示片刻,但警报会自动关闭。
有没有办法让警报保持打开状态?
【问题讨论】:
标签: sweetalert2
在preConfirm 获取之后,then 方法中是否有可能没有关闭 Sweetalert (2)?如果请求返回特定的内容,我只想让 Swal 保持打开状态,但执行 Swal.showValidationMessage() 而不是打开一个全新的警报。在then 方法中执行验证消息时,它会显示片刻,但警报会自动关闭。
有没有办法让警报保持打开状态?
【问题讨论】:
标签: sweetalert2
如果我没记错的话,您应该在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.
}
});
【讨论】: