我正在使用SweetAlert和Symfony,我希望用戶在完成刪除操作之前進行確認。Sweet Alert不會等到用戶點擊確定
發生什麼事情是當用戶點擊刪除按鈕SweetAlert彈出,然後立即消失,項目被刪除。
我不希望項目被刪除,直到用戶從SweetAlert彈出窗口中單擊確定。
這是我的設置。
JS:
function myFunction() {
swal({
title: "Are you sure?",
text: "You will not be able to recover this Job Title!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
closeOnCancel: false
}, function (isConfirm) {
if (isConfirm){
swal("Deleted!", "Job Title has been deleted.", "success");
} else {
swal("Cancelled", "Job Title was not deleted.", "error");
}
});
};
HTML:
<a href="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}" onclick="myFunction()" class="btn btn-white" data-toggle="tooltip" data-toggle="tooltip" data-placement="top" title="Delete">
<i class="fa fa-trash"></i>
</a>
我如何刪除操作等待,直到用戶點擊OK?
編輯:我結束了使用$.post
和移動href
到data-
這樣的:
<a href="#" onclick="myFunction(this)" data-url="{{ path('setup_jobtitles_delete', {'id': jobTitle.id}) }}" class="btn btn-white" data-toggle="tooltip" data-toggle="tooltip" data-placement="top" title="Delete">
<i class="fa fa-trash"></i>
</a>
function myFunction(btn) {
var url = $(btn).data('url');
swal({
title: "Are you sure?",
text: "You will not be able to recover this Job Title!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function() {
$.post(url, function(){
swal("Deleted!", "Job Title has been deleted.", "success");
$(btn).parents('tr').remove();
});
});
};
它與Symfony無關。你必須防止默認的瀏覽器行爲(通過返回false onclick即) – Brewal
所以我改變了我的onclick這個:'myFunction();返回false;'現在不刪除任何東西。我可以點擊確定,但不會刪除。 – iamthestreets
你在哪裏閱讀sweetalert允許你刪除任何東西?這只是簡單的js警報功能漂亮的包裝。你可以在你的'isConfirm'函數中添加ajax調用來刪除Symfony後端的操作。 – malcolm