Laravel带有Sweet Alert的删除功能

时间:2018-10-24 08:32:27

标签: javascript laravel sweetalert2

所以我在laravel 5.7上使用了甜警报

我需要帮助,因为单击甜美警报确认按钮后,该功能似乎无法正常工作。

我的html:

<button rel="{{ $mail->id }}"" rel1="delete-mail" href="javascript:" 
class="deleteMail btn btn-danger btn-xs">Delete</button></td>

我的脚本:

    $(".deleteMail").click(function(){
    var id = $(this).attr('rel');
    var deleteFunction = $(this).attr('rel1');
    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        confirmButtonText: 'Yes, delete it!',
        buttonsStyling: false

    },
    function(){
      window.location.href='/admin/'+deleteFunction+'/'+id;
    });

    });

路线:

public function delete($id = null){

    if(!empty($id)){
        Mail::where(['id'=>$id])->delete();
        return redirect()->back()->with('flash_message_success','Surat berhasil dihapus!!');
    }

}

我尝试了window.location.href 没有甜蜜警报功能,并且效果很好。

但是,当我使用甜警报时,它根本不起作用。

我在console log中找不到任何错误。请帮忙。

2 个答案:

答案 0 :(得分:1)

尝试。然后功能。 例如:

  $(".deleteMail").click(function(){
  var id = $(this).attr('rel');
  var deleteFunction = $(this).attr('rel1');
  swal({
      title: 'Are you sure?',
      text: "You won't be able to revert this!",
      type: 'warning',
      showCancelButton: true,
      confirmButtonClass: 'btn btn-success',
      cancelButtonClass: 'btn btn-danger',
      confirmButtonText: 'Yes, delete it!',
      buttonsStyling: false

    }).then((isConfirm) => {


   if (isConfirm){
       window.location.href='/admin/'+deleteFunction+'/'+id;
     }
    });
    });

答案 1 :(得分:0)

尝试

 $(".deleteMail").click(function(){
    var id = $(this).attr('rel');
    var deleteFunction = $(this).attr('rel1');
    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        confirmButtonText: 'Yes, delete it!',
        buttonsStyling: false

    },function(isConfirm){
        alert('ok');
    });
    $('.swal2-confirm').click(function(){
        window.location.href='/admin/'+deleteFunction+'/'+id;
    });
});