我在视图中列出了类别。视图中还有一个删除类别按钮,它可以工作并在单击时删除该类别。
我想要做的是删除一个类别,一个甜蜜的警告对话框,弹出并要求确认。如果确认,它应该转到定义的路线并删除类别。
删除链接的定义如下:
LocateUsingStrings
并且脚本定义如下:
<a id="delete-btn" href="{{ route('admin.categories.destroy', $category->id) }}" class="btn btn-danger">Delete</a>
但是,当我点击删除按钮时,它会删除该类别,但不会显示甜蜜警报消息。
路线定义如下:
<script>
$(document).on('click', '#delete-btn', function(e) {
e.preventDefault();
var link = $(this);
swal({
title: "Confirm Delete",
text: "Are you sure to delete this category?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: true
},
function(isConfirm){
if(isConfirm){
window.location = link.attr('href');
}
else{
swal("cancelled","Category deletion Cancelled", "error");
}
});
});
</script>
,控制器功能定义为:
Route::get('/categories/destroy/{category}', [
'uses' => 'CategoriesController@destroy',
'as' => 'admin.categories.destroy',
]);
任何帮助将不胜感激。谢谢。
答案 0 :(得分:3)
试试这个:
<script>
var deleter = {
linkSelector : "a#delete-btn",
init: function() {
$(this.linkSelector).on('click', {self:this}, this.handleClick);
},
handleClick: function(event) {
event.preventDefault();
var self = event.data.self;
var link = $(this);
swal({
title: "Confirm Delete",
text: "Are you sure to delete this category?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: true
},
function(isConfirm){
if(isConfirm){
window.location = link.attr('href');
}
else{
swal("cancelled", "Category deletion Cancelled", "error");
}
});
},
};
deleter.init();
</script>
编辑:根据您在@ kalyan-singh-rathore的回答中的评论,我认为您没有在您的刀片模板中正确注入脚本。如果您要扩展基本布局,请确保已包含该脚本或从子布局中获取该脚本。
答案 1 :(得分:0)
以下面的方式写锚。
<a href="#" customParam="URL">Delete</a>
现在在URL中选择将href更改为customParam。
function (isConfirm) {
if (isConfirm) {
window.location = link.attr('customParam');
} else {
swal("cancelled", "Category deletion Cancelled", "error");
}
}
基本上在你的情况下,点击href和事件lisner。
答案 2 :(得分:0)
试试这个为我解决的问题:)
document.querySelector('#promote').addEventListener('submit', function (e) {
let form = this;
e.preventDefault(); // <--- prevent form from submitting
swal({
title: "Promote Students",
text: "Are you sure you want to proceed!",
type: "warning",
showCancelButton: true,
// confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, I am sure!',
cancelButtonText: "No, cancel it!",
closeOnConfirm: false,
closeOnCancel: false,
dangerMode: true,
}).then((willPromote) => {
e.preventDefault();
if (willPromote.value) {
form.submit(); // <--- submit form programmatically
} else {
swal("Cancelled", "No students have been promoted :)", "error");
e.preventDefault();
return false;
}
});
});