添加 sweetalert2 确认删除 php 中没有 json 数据

add sweetalert2 confirm delete in php without json data

如何在按钮 href 中使用带删除功能的 sweetalert2? 我有这样的按钮

<button type="button" id="btnDelete" href=" <?php echo site_url('administrator/master/delete/' . $a->idDept); ?>" class="btn btn-danger fa fa-trash but" data-toggle="tooltip" data-placement="top" title="Delete"></button>

 $('.btnDelete').click(function() {
       swal.fire({
           title: 'Are you sure?',
           text: "It will permanently deleted !",
           type: 'warning',
           showCancelButton: true,
           confirmButtonColor: '#3085d6',
           cancelButtonColor: '#d33',
           confirmButtonText: 'Yes, delete it!'
       }).then(function() {
           swal.fire(
               'Deleted!',
               'Your file has been deleted.',
               'success'
           );
       });
   });

sweetalert2 工作但不删除数据

您需要在 Swal.fire 调用中传递 fetch/ajax 方法。

  $('.btnDelete').click(function(e) {
  e.preventDefault();
  var url = $(this).attr('href');
  Swal.fire({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  icon: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!',
  preConfirm: () => {
    return fetch(url)
      .then(response => {
      console.log(response);
        if (!response.ok) {
          throw new Error(response.statusText)
        }
        return response.json()
      })
      .catch(error => {
        Swal.showValidationMessage(
          `Request failed: ${error}`
        )
      })
  }
}).then((result) => {
  if (result.isConfirmed) {
    Swal.fire(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    )
  }
});
});

使用此示例 codepan 并根据需要进行修改。 https://codepen.io/ympervej/pen/wvJQyOr