javascript - sweetalert 删除 确认 laravel
问题描述
我在laravel中遇到了一个问题,用于确认sweetalert删除的帖子
<script>
!function ($) {
"use strict";
var SweetAlert = function () {
};
//examples
SweetAlert.prototype.init = function () {
$('.sa-remove').click(function () {
swal({
title: "are u sure?",
text: "lorem lorem lorem",
type: "error",
showCancelButton: true,
confirmButtonClass: 'btn-danger waves-effect waves-light',
confirmButtonText: "Delete",
cancelButtonText: "Cancel",
closeOnConfirm: true,
closeOnCancel: true
},
function(){
window.location.href = "{{ route('panel.posts.remove',$post->id) }}";
});
});
},
//init
$.SweetAlert = new SweetAlert, $.SweetAlert.Constructor = SweetAlert
}(window.jQuery),
//initializing
function ($) {
"use strict";
$.SweetAlert.init()
}(window.jQuery);
</script>
但是我foreach
在视图中有一个,它只是通过了最后一个foreach
帖子id
,当我想删除例如表中的第二个帖子时,最后一个删除了!
这是表格:
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
<th>Author</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->body }}</td>
<td>{{ $post->user->name }}</td>
<td>
<a href="#" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a>
</td>
</tr>
@endforeach
</tbody>
我当然是新来的!
解决方案
您正在删除错误的模态对象。首先您应该向链接按钮添加一个数据属性
<a href="#" data-id="{{$post->id}}" class="sa-remove"><button class="wave-effect btn btn-danger btn-bordred wave-light"><i class="fa fa-times"></i></button></a> code here
然后在您的 javascript 代码中检索属性值并更改 url。
$('.sa-remove').click(function () {
var postId = $(this).data('id');
swal({
title: "are u sure?",
text: "lorem lorem lorem",
type: "error",
showCancelButton: true,
confirmButtonClass: 'btn-danger waves-effect waves-light',
confirmButtonText: "Delete",
cancelButtonText: "Cancel",
closeOnConfirm: true,
closeOnCancel: true
},
function(){
window.location.href = "your-url/" + postId;
}); here
推荐阅读
- javascript - 打开带有外部链接的手风琴常见问题解答内容
- angular - 架构验证失败,错误优化。样式应该是布尔值
- eslint - 删除添加任何规则后是否需要重新加载 eslint?
- powershell - 如何在 PowerShell 中将文件从 EFS (WinSCP) 上传到 WebHDFS (Hue/Cloudera)?
- docusignapi - 如何在 DocuSign 中创建具有 sharedSigningGroup 类型的用户组组?
- javascript - 如何使用 jQuery 删除元素所需的内容?
- wpf - 如何格式化消息以使其显示在 Visual Studio 的“XAML 绑定失败”-Toolwindow 中?
- python - Python - 无法并行运行多个查询
- c++ - c++ 中是否有任何解决方法来模拟/从最终类派生
- r - 字符串按管道拆分并放入列中