未捕获的SweetAlert错误:意外的第二个参数?

12

我在使用sweetalert时遇到了问题,我希望在按钮点击时显示确认框警报,但它不起作用。

这是我的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:

<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>

所有必需的文件已导入。

1个回答

19

您正在使用的代码是早于最新版本2的。请阅读从1.X版本升级

您应该使用promise来跟踪用户交互。

更新后的代码

$(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!");
          }
        });
    });
});

注意事项

  • type已被替换为icon选项。
  • 使用only buttons替换了showCancelButton、CancelbuttonText、confirmButtonText和confirmButtonColor。
  • 将dangerMode设置为true可使确认按钮变为红色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接