首页 > 解决方案 > 删除表中的行时如何使用sweetalert

问题描述

我想在链接我的控制器之前删除表中的数据时使用sweetalert。但它只调用我的控制器并且不显示任何警报。

<a href="Table/Delete/<?php echo ($u->ID) ?>">
     <button id="a" type="button" class="btn btn-danger"> <i class="glyphicon glyphicon-trash"></i> Delete </button>
</a>

js

<script>
   $('#a').on('click',function(event){
      event.preventDefault();
         swal({
            title: "Are you sure?",
            text: "Once deleted, you will not be able to recover this imaginary file!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
         })
        .then((willDelete) => {
          if (willDelete) {
             swal("Poof! Your imaginary file has been deleted!", {
             icon: "success",
          });
          } else {
            swal("Your imaginary file is safe!");
            }
          });
   })
</script>

标签: javascriptsweetalert

解决方案


不要使用<a href="...">链接

HTML 应该像 ..

<div>
  <button id="a"></button>
</div>

js应该如下

<script>
   $('#a').on('click',function(event){
      event.preventDefault();
         swal({
            title: "Are you sure?",
            text: "Once deleted, you will not be able to recover this imaginary file!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
         })
        .then((willDelete) => {
          if (willDelete) {
             # delete row
             # show swal and redirect to your link
          } else {
            swal("Your imaginary file is safe!");
            }
          });
   })
</script>

推荐阅读