如何解决添加sweetalert2在没有json数据的情况下在php中确认删除
如何在按钮 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'
)
}
});
});
使用此示例代码盘并根据需要对其进行修改。 https://codepen.io/ympervej/pen/wvJQyOr
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。