2017-10-11 20 views
1

私はsweetalertに問題がある、私は、ボタンのクリックで確認ボックスの警告を表示したいと思いますが、それはこれが私のJSコードである未知SweetAlert:予期しない第2引数ですか?

を働いていない:

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

使用しているコードは、あなたがユーザーの操作を追跡するためにpromiseを使用する必要があります​​

をよく読んでください前から最新のバージョン2です。

更新されたコード

$(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。
関連する問題