【发布时间】:2018-02-06 15:59:34
【问题描述】:
简介
大家好!我是 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'
})
}
});
});
}
当我点击删除按钮时:
显示确认型号:
然后我会点击取消按钮。但是在单击取消按钮后,从数据库中删除了数据。为什么在这两种情况下都删除了条目?
【问题讨论】:
-
在 swal 之后的 then 函数中,您需要检查是否按下了 ok 或 cancel 并且只有在按下 ok 时才删除内容。查看“使用承诺”here 下的示例
-
我看到了@James 文档,但我不明白要在此处查看哪些内容以进行点击。如何检查按下了哪个按钮?在 javascript 中有函数
button = confirm('Confirm message!')并且可以使用它if(button == true)但在 sweetalerts 我不能使用检查点击按钮。 -
你的使用方式,
then是错误的,它基本上是说,每当sweetAlert显示或显示完,然后从服务器删除数据,你必须检查按钮是否被按下或不是。
标签: javascript laravel sweetalert sweetalert2