使用sweet alert确认删除记录

Using sweet alert for confirmation of deletion record

如何将这个正确的格式转换为使用 sweet alert。

我在网站上广泛使用此功能,如果使用 sweetalert2 而不是通用的 javascript 警报会很好。

https://sweetalert2.github.io/

jQueryAjaxDelete = form => {     
 if (confirm('Are you sure to delete this record ?')) {
    try {
        $.ajax({
            type: 'POST',
            url: form.action,
            data: new FormData(form),
            contentType: false,
            processData: false,
            success: function (res) {
                $('#view-all').html(res.html);
            },
            error: function (err) {
                console.log(err)
            }
        })
    } catch (ex) {
        console.log(ex)
    }
}

  //prevent default form submit event
  return false;
}

提前致谢

使用 Sweetalert 制作一个新的确认提示,并删除 if 条件中的 vanilla js 确认提示。如果确认为真,则进行删除 ajax 调用。

见下方代码

jQueryAjaxDelete = form => {
  // Modify as you wish
  const sweetOptions = {
    title: "Delete",
    text: "Are you sure  you want to delete this record?",
    type: "warning",
    showCancelButton: true,
    confirmButtonText: "Yes!",
    cancelButtonText: "Cancel!",
    closeOnConfirm: false,
    closeOnCancel: false 
  }
  
  swal(sweetOptions, (isConfirmed) => {
    if (isConfirmed) {
      $.ajax({
          type: 'POST',
          url: form.action,
          data: new FormData(form),
          contentType: false,
          processData: false,
          success: function (res) {
              $('#view-all').html(res.html);
              
              // Fire a delete alert with SweetAlert here
              swal("Deleted!", "Deleted!", "success");
          },
          error: function (err) {
              console.log(err)
              swal("Cancelled", "Delete failed", "error");
          }
      })
    } else {
        swal("Cancelled", "Delete cancelled", "error");
    }
  }
   //prevent default form submit event
   return false;
 }