大家好!我是javascript的首发。我在项目中使用Laravel + DataTables。现在我使用sweetalert2而不是默认确认javascript。
我在这里调用我的javascript方法:
<a href="#" onclick="deleteData('.$contact->id.')" class="btn btn-danger btn-xs">
<i class="fa fa-trash"></i>
<span>Delete</span>
</a>
这是我的javascript函数:
function deleteData(id){
var csrf_token = $('meta[name="csrf-token"]').attr('content');
swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
cancelButtonColor: '#d33',
confirmButtonColor: '#3085d6',
confirmButtonText: 'Yes, delete it!'
}).then(function(){
$.ajax({
url: "{{url('contact')}}" + '/' + id,
type: "POST",
data: {'_method' : 'DELETE', '_token' : csrf_token},
success: function(data){
contacts.ajax.reload();
swal({
title: 'Success!',
text: 'Data has been deleted!',
type: 'success',
timer: '1500'
})
},
error: function(){
swal({
title: 'Oops...',
text: 'Something went wrong!',
type: 'error',
timer: '1500'
})
}
});
});
}
当我点击删除按钮时:
显示确认模型:
然后,我点击取消按钮。但点击取消按钮后从数据库中删除数据。为什么在这两种情况下都删除了条目?
答案 0 :(得分:1)
您忘了检查已解决的承诺的结果:
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@7"></script>
$fetch_style