gpt4 book ai didi

javascript - 无法阻止甜蜜警报关闭

转载 作者:行者123 更新时间:2023-11-30 13:53:05 25 4
gpt4 key购买 nike

我试图在关闭 SweetAlert 之前验证 email 字段,但由于某种原因 SweetAlert 在发现错误后关闭:

Swal.fire({
title: title,
html: template,
preConfirm: function (email) {
email = '';
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (email === '') {
alert("err");
reject('err')
} else {
resolve()
}
}, 1000)
}).catch(err => alert("error catched"))
},
});

如何防止 SweetAlert 模式在捕获到错误时关闭?

谢谢

最佳答案

这是一个功能齐全的示例(用于解决您的特定问题):

<html>
<body>
<div class="showDialogButton">Show dialog</div>

<script src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script type="text/javascript">

$(() => {

const title = "Hi"
const template = `<b>hello</b> world`

$(".showDialogButton").click(() => {
Swal.fire({
title: title,
html: template,
preConfirm: function (email) {
email = '';
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (email === '') {

reject('Email is empty!')

} else {

resolve()
}
}, 1000)
}).catch(err => {
alert(`error: ${err}`)
return false
})
},
});
})
})
</script>
</body>
</html>

默认行为是关闭对话框。为了覆盖它,您必须从传递给 .catch 的函数中返回 false

来自 https://sweetalert2.github.io/#configuration

Argument: preConfirm

Default value: null

Description: Function to execute before confirm, may be async (Promise-returning) or sync. Returned (or resolved) value can be:

  • false to prevent a popup from closing
  • anything else to pass that value as the result.value of Swal.fire()
  • undefined to keep the default result.value

关于javascript - 无法阻止甜蜜警报关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57842624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com