我在Sweetalert中遇到了这个问题-我有以下代码,但是确认模式是无用的,因为请求通过了它,用户甚至没有时间来决定。我必须弄清楚它才能停止执行请求并等待用户决定(确定/取消)。
代码如下:
<a href="{{route('notes.destroy', $note->id)}}"
data-id="{{$note->id}}" onclick="confirmDelete('{{$note->id}}')" type="submit">
<span class="badge badge-danger">Delete</span></a>
这是jQuery(我认为是问题所在):
<script>
function confirmDelete(item_id) {
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover it!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
$('#' + item_id).submit();
} else {
swal("Cancelled Successfully");
}
});
}
</script>
答案 0 :(得分:1)
在刀片文件中:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.js"></script>
<form id="delete_from_{{$note->id}}" method="POST" action="{{ route('post.destroy', $note->id) }}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<a href="javascript:void(0);" data-id="{{$note->id}}" class="_delete_data">
<span class="badge badge-danger">Delete</span>
</a>
</div>
</form>
js代码:
<script>
$(document).ready(function(){
$('._delete_data').click(function(e){
var data_id = $(this).attr('data-id');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
$(document).find('#delete_from_'+data_id).submit();
}
})
});
});
</script>
答案 1 :(得分:0)
如果我正确理解了您的问题,则表示Sweetalert模态没有为您提供onclick事件之后是继续还是中止决策的任何选择。尝试如下添加“ showCancelButton”和“ confirmButtonText”,看看是否有任何区别。
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover it!",
icon: "warning",
showCancelButton: true,
confirmButtonText: 'ok'
}).then((willDelete) => {
if (willDelete) {
$('#' + item_id).submit();
} else {
swal("Cancelled Successfully");
}
});
答案 2 :(得分:0)
防止使用preventDefault()
function confirmDelete(event,item_id) {
event.preventDefault();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover it!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
$('#' + item_id).submit();
} else {
swal("Cancelled Successfully");
}
});
}
<a href="{{route('notes.destroy', $note->id)}}"
data-id="{{$note->id}}" onclick="confirmDelete(event,'{{$note->id}}')" type="submit">
<span class="badge badge-danger">Delete</span></a>
and here's the jQuery (which I think it's the problem):