我得到了这个函数,它覆盖了我的本机js警报:
function alert(message, title = 'Test', type = 'info')
{
// event.preventDefault();
if(typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then((result) => {
return result.value;
});
}
else {
alert(message);
}
}在我的PHP函数的末尾,我有一个警告(‘成功’),然后我重定向到另一个页面。对于本机JS警报,它等待我单击OK按钮以继续。现在有了这个swal函数,它可以立即显示警报和重定向。有没有一种方法可以避免这种行为,并在不更改函数签名的情况下像本机警报一样工作?
发布于 2018-11-14 00:42:13
不同之处在于alert()是模态的。这意味着它会阻塞所有其他输入和输出,直到它被清除。甜蜜警报不是。
您可以通过使用回调函数使其以类似的方式运行,该回调函数是在单击甜蜜警报中的OK按钮时执行的。您可以通过将函数传递给alert(),然后在then()块中调用它来完成此操作,如下所示:
function alert(message, title = 'Test', type = 'info', callback) {
if (typeof(swal) != 'undefined') {
swal({
html: message,
title: title,
type: type,
width: '24rem',
}).then(() => {
callback && callback();
});
} else {
alert(message);
callback && callback();
}
}
// example usage:
alert('foo bar', 'title', 'info', function() {
window.location.assign('somewhere_else.php');
});https://stackoverflow.com/questions/53285512
复制相似问题