无法阻止甜蜜警报关闭
Cannot prevent sweet alert from closing
我试图在关闭 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>
默认行为是关闭对话框。为了覆盖它,您必须 return false
从您传递给 .catch
.
的函数
来自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
我试图在关闭 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>
默认行为是关闭对话框。为了覆盖它,您必须 return false
从您传递给 .catch
.
来自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
ofSwal.fire()
undefined
to keep the defaultresult.value