未捕获的SweetAlert:意外的第二个论点?

Ази*_*ани 9 javascript sweetalert

我有一个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;
    }
});
});
});
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

导入所有必需的文件.

5le*_*ess 12

您使用的代码来自最新版本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!");
          }
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

笔记

  • 类型已替换为图标选项.
  • 仅使用按钮替换了showCancelButton,CancelbuttonText,confirmButtonText和confirmButtonColor.
  • dangerMode:如果确认按钮为红色,则为true.