我想在我的ASP.NET MVC应用程序中显示一条自定义确认消息。
经过一番搜索,我发现了SweetAlert,这是一个非常好的工具。
https://sweetalert2.github.io/
我想在一个js文件中定义一个javascript方法,它会调用这个甜蜜的警告来显示一个对话框。
但是文档不会等待客户端的响应。
为了演示我的意思,我添加了下面的代码。
代码alert(“its‘t wait");在sweetalert显示其消息之前执行。
我的目标是添加一个自定义的javascript文件,并定义一个简单的函数来调用并返回true或false,以避免在每个确认案例中输入下面所有的代码。因为它不等待客户端的交互,所以我不知道这是否可能。
有什么想法吗?
<html>
<head>
<script src="SweetAlert.js"></script>
</head>
<body>
<button onclick="customConfirm();">Confirm</button>
</body>
</html>
<script type="text/javascript">
function customConfirm(){
Swal.fire({
title: 'myTitle',
text: 'my Question',
type: 'question',
showCancelButton: true,
confirmButtonColor: 'rgb(181, 212, 83)',
cancelButtonColor: '#d33',
confirmButtonText: 'YES'
}).then((result) => {
if (result.value) {
return true;
}
else {
return false;
}
});
alert("doesn't wait.");
}
</script>发布于 2019-12-05 23:06:53
您应该在回调中执行所有检查。
function customConfirm(callback){
Swal.fire({
title: 'myTitle',
text: 'my Question',
type: 'question',
showCancelButton: true,
confirmButtonColor: 'rgb(181, 212, 83)',
cancelButtonColor: '#d33',
confirmButtonText: 'YES'
}).then((result) => {
// Do some stuff and call the callback
callback();
if (result.value) {
return true;
}
else {
return false;
}
});在另一个文件中:
customConfirm(function() {
// Put some stuff you want to do
}); 或者:
function callbackDoSomething() {}
customConfirm(callbackDoSomething);https://stackoverflow.com/questions/59197920
复制相似问题