2017-10-11 442 views
1

我有sweetalert一个问题,我想显示按钮点击确认框警报,但它不工作Uncaught 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> 

导入所有必需的文件。

回答

3

您正在使用的代码是从之前的最新版本2.请​​

读了您应该使用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!"); 
      } 
     }); 
    }); 
}); 

注意

  • 类型已被替换的图标选项。
  • 只有按钮替换showCancelButton,CancelbuttonText,confirmButtonText和confirmButtonColor。
  • dangerMode:true将确认按钮设为红色。