我的视图中列出了类别和子类别。我还有一个删除类别按钮,它可以工作,并删除该类别及其所有子类别。我想要做的是在删除父类别之前,我想要弹出甜蜜警报按钮并询问您是否确定要删除此类别,使用是和否按钮?我知道我必须使用ajax才能完成,但是我用ajax不是很好。现在这就是我所拥有的,当我点击删除按钮时,它会删除类别,但不会显示甜蜜警报消息。
Route.php
/** Delete a category **/
Route::delete('admin/categories/delete/{id}', [
'uses' => '\App\Http\Controllers\CategoriesController@deleteCategories',
'as' => 'admin.category.delete',
'middleware' => ['auth'],
]);
CategoriesController.php:
class CategoriesController extends Controller {
/** More function here, just hidden for ease right now **/
/**
* Delete a Category
*
* @param $id
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteCategories($id) {
// Find the category id and delete it from DB.
Category::findOrFail($id)->delete();
// Then redirect back.
return redirect()->back();
}
}
我的表格:
@foreach ($categories as $category)
{{ $category->category }}
<form method="post" action="{{ route('admin.category.delete', $category->id) }}" class="delete_form">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button id="delete-btn">
<i class="material-icons delete-white">delete_forever</i>
</button>
</form>
@endforeach
我的甜蜜警报:
<script type="text/javascript">
$('#delete-btn').on('click', function(e){
e.preventDefault();
var self = $(this);
swal({
title: "Are you sure?",
text: "All of the sub categories will be deleted also!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: true
},
function(isConfirm){
if(isConfirm){
swal("Deleted!","Category and all sub categories deleted", "success");
setTimeout(function() {
self.parents(".delete_form").submit();
}, 2000);
}
else{
swal("cancelled","Your categories are safe", "error");
}
});
});
</script>
答案 0 :(得分:1)
让它工作,我必须为我的甜蜜警报JavaScript做这个:
$(document).on('click', '#delete-btn', function(e) {
e.preventDefault();
var self = $(this);
swal({
title: "Are you sure?",
text: "All of the sub categories will be deleted also!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: true
},
function(isConfirm){
if(isConfirm){
swal("Deleted!","Category and all sub categories deleted", "success");
setTimeout(function() {
self.parents(".delete_form").submit();
}, 1000);
}
else{
swal("cancelled","Your categories are safe", "error");
}
});
});
感谢maximl337的帮助!
答案 1 :(得分:1)
将click事件绑定到DOM而不是元素。
import json
def get_call_back(request):
# Do something to return dictionary same as {'abc': xyz}
json_data = json.dumps(data)
return HttpResponse(json_data, content_type='application/json')
这也解决了动态加载元素的问题,例如:在ajax调用之后渲染动作按钮。
答案 2 :(得分:0)
在DOM节点上使用submit事件而不是jquery对象:
$('.delete_form')[0].submit();