在 AJAX 中删除带有甜蜜警报确认的查询

Delete query with sweet alert confirmation in AJAX

我有 CRUD table。当我点击删除时,出现一个确认框,但我想用sweet alert

更改此确认框

javascript

function delete_unit(id){
        if(confirm('Hapus Data?'))// <== I wanna change it with sweetalert
        {
            $.ajax({
                url : "<?php echo base_url('C_unit/delete')?>/" + id,
                type : "POST",
                dataType : "JSON",
                success : function(data)
                {
                    location.reload();
                },
                error : function(jqXHR, textStatus, errorThrown)
                {
                    alert('Gagal menghapus data');
                }
            });
        }
    }

你能试试下面的代码吗。还有详细帮助的文档 https://sweetalert.js.org/guides/

    function delete_unit(id){       
          swal({
          title: "Are you sure?",
          text: "Once deleted, you will not be able to recover this data!",
          icon: "warning",
          buttons: true,
          dangerMode: true,
        })
        .then((willDelete) => {
  if (willDelete) {
            $.ajax({
                        url : "<?php echo base_url('C_unit/delete')?>/" + id,
                        type : "POST",
                        dataType : "JSON",
                        success : function(data)
                        {
                            location.reload();
                        },
                        error : function(jqXHR, textStatus, errorThrown)
                        {
                            alert('Gagal menghapus data');
                        }
                 });
                }
        });
     }

这是一个有效的 Fiddle https://jsfiddle.net/s7zd9822/