sweetalert2 - 在“then”中显示验证消息,而不是自动关闭 Swal
问题描述
获取后是否可以在方法中关闭Sweetalert (2)?如果请求返回特定的内容,我只想让 Swal 保持打开状态,而不是打开一个全新的警报。在方法中执行验证消息时,它会显示片刻,但警报会自动关闭。then
preConfirm
Swal.showValidationMessage()
then
有没有办法让警报保持打开状态?
解决方案
If I'm not mistaken, you should be calling the Swal.showValidationMessage()
function inside the preConfirm()
option, not in the then()
promise return.
Would validating whatever input you're looking for inside the preConfirm()
just work fine?
Example:
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.
}
});
推荐阅读
- corda - 无法加载 CordaService
- python-3.x - Django AttributeError:模块'clients'没有属性'models'
- ios - 如何根据设备屏幕大小调整集合视图中单元格的大小?
- php - 对 DataTable 的 DateTime 字段进行排序
- vba - 如何使用 VBA 保存 Excel 加载项
- java - Gradle 使用 2 个不同的 JDK
- sql-server - Sql server 索引使用取决于列值
- css - 如何更改禁用光标
- apache-camel - 骆驼豆绑定:从变量中设置参数
- javascript - 防止客户端编辑功能的最佳实践?