javascript 未捕获的 SweetAlert:意外的第二个参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46690732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught SweetAlert: Unexpected 2nd argument?
提问by Азиз Чеграни
I have a problem with sweetalert, I would like to show the confirm box alert on button click but it's not working
我有sweetalert 问题,我想在单击按钮时显示确认框警报,但它不起作用
This is my JS code:
这是我的JS代码:
$(document).ready(function(){
$('[data-confirm]').on('click', function(e){
e.preventDefault(); //cancel default action
//Recuperate href value
var href = $(this).attr('href');
var message = $(this).data('confirm');
//pop up
swal({
title: "Are you sure ??",
text: message,
type: "warning",
showCancelButton: true,
cancelButtonText: "Cancel",
confirmButtonText: "confirm",
confirmButtonColor: "#DD6B55"},
function(isConfirm){
if(isConfirm) {
//if user clicks on "confirm",
//redirect user on delete page
window.location.href = href;
}
});
});
});
HTML:
HTML:
<a data-confirm='Are you sure you want to delete this post ?'
href="deletePost.php?id=<?= $Post->id ?>"><i class="fa fa-trash">
</i> Delete</a>
All required files are imported.
导入所有必需的文件。
回答by 5less
The code you are using is from prior the latest version 2. Please read up on Upgrading from 1.X.
您使用的代码来自最新版本 2 之前的代码。请阅读从 1.X 升级。
You should use promiseto keep track of user interaction.
您应该使用promise来跟踪用户交互。
Updated code
更新代码
$(document).ready(function(){
$('[data-confirm]').on('click', function(e){
e.preventDefault(); //cancel default action
//Recuperate href value
var href = $(this).attr('href');
var message = $(this).data('confirm');
//pop up
swal({
title: "Are you sure ??",
text: message,
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal("Poof! Your imaginary file has been deleted!", {
icon: "success",
});
window.location.href = href;
} else {
swal("Your imaginary file is safe!");
}
});
});
});
Notes
笔记
- type have been replaced with icon option.
- Replaced showCancelButton, CancelbuttonText, confirmButtonText and confirmButtonColor with only buttons.
- dangerMode: true to make the confirm button red.
- 类型已替换为图标选项。
- 仅用按钮替换了 showCancelButton、CancelbuttonText、confirmButtonText 和 confirmButtonColor。
- riskMode: true 使确认按钮变为红色。

