首页 > 解决方案 > 使用 sweetalert2 进行软删除的正确方法

问题描述

大家好,我正在使用 sweetalert2,这已经是功能,但有一些错误需要修复。

错误:

  1. 当我单击确定时,location.reload()将触发而不单击“确定”。

  2. 当我单击取消时,模式仍处于弹出模式。

这是我的脚本。

$('body').on('click','.delete-category',function(){
            deleteCategory();
        });

        function deleteCategory(id){

           var id = $('body').find('#categoryTable tbody tr.selected .catId').val();
           console.log(id);

            swal({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then(function(isConfirm){
                if (isConfirm) {
                    $.ajax({
                        url: `/asset/category-delete/${id}`,
                        method: 'DELETE',
                        data: { _token: '{{ csrf_token()}}' },
                        success: function(data){
                            swal(
                                'Deleted!',
                                'Your file has been deleted.',
                                'success' 
                            );
                            location.reload();
                            },
                        error: function(err){
                            swal('Error!','Please report this issue.', 'error');
                        }
                    });
                } else {
                swal("Cancelled","", "error");
                }
            });
        }

寻求增强。

标签: jquerylaravel-5sweetalert2

解决方案


关于你的错误 #1:如果你想在 swal 交互之后执行,你必须放置location.reload();一个方法。then()

success: function(data){
  swal(
    'Deleted!',
    'Your file has been deleted.',
    'success' 
  ).then(()=>{
    location.reload();
  });
},

关于您的错误#2:我无法弄清楚您在说什么“删除模式” ...


推荐阅读