甜蜜警报仅删除第一行

时间:2017-09-13 06:24:14

标签: laravel sweetalert

在我的laravel项目中,我使用甜蜜警报工作正常,但它只删除了我的第一行记录我不知道问题是什么

这是我的表格

<td>                                    
<form id="myform" class="delete-photo" method="POST" action="{{route('testimony.destroy', $testimony->id)}}">
 <input type="hidden" name="_method" value="delete">
                                            {{ csrf_field() }}
    <div class="form-group">
        <button type="submit" data-photo-id="{{$testimony->id}}"
                class="submitdel btn btn-danger"
        >Delete</button>
    </div>
</form>

这是我的脚本

$('.delete-photo').click(function(e) {
    e.preventDefault(); // Prevent the href from redirecting directly
    var linkURL = $(this).attr("action");
    warnBeforeRedirect(linkURL);
});

function warnBeforeRedirect(linkURL) {
    swal({
            title: "Are you sure?",
            text: "You will not be able to recover this file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#5c5856",
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel plx!",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function(isConfirm) {
            if (isConfirm) {
                document.getElementById("myform").submit();
            } else {
                swal("Cancelled", "Your file is safe :)", "error");
            }
        }
    );
}

1 个答案:

答案 0 :(得分:0)

这是因为您的表单每次都传递相同的ID。如果你在你的记录中循环,那么表格必须循环思考。但这不是好习惯。我建议你使用

<button type="submit" data-photo-id="{{$testimony->id}}" class="submitdel btn btn-danger">Delete</button>
循环

然后 $ testimony-&gt; id 与它自己的来自数据库的ID不同。然后在点击事件处理程序上使用Javascript获取 data-photo-id ,然后将此ID传递给您的控制器并发布网址。

好的支持您从数据库获取动态数据并将其显示为

<table>
  <tr>
    <td>name</td>
    <td>email</td>
    <td><button type="submit" data-photo-id="1" class="submitdel btn btn-danger">Delete</button></td>
  </tr>
  <tr>
    <td>name</td>
    <td>email</td>
    <td><button type="submit" data-photo-id="2" class="submitdel btn btn-danger">Delete</button></td>
  </tr>
  <tr>
    <td>name</td>
    <td>email</td>
    <td><button type="submit" data-photo-id="3" class="submitdel btn btn-danger">Delete</button></td>
  </tr>
</table>

然后使用这种方式获取data-photo-id(用动态ID替换它)

<script>
$(function(){
  $('.submitdel').on('click',function(){
    // store current button into variable
    var id = $(this).data('photo-id');
    warnBeforeRedirect(id);
  })
})

function warnBeforeRedirect(id) {
    swal({
            title: "Are you sure?",
            text: "You will not be able to recover this file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#5c5856",
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel plx!",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function(isConfirm) {
            if (isConfirm) {
                window.location = 'http://yoursite.com/controllername/'+id
            } else {
                swal("Cancelled", "Your file is safe :)", "error");
            }
        }
    );
}
</script>

并将 window.location 替换为您要使用的网址。希望你现在得到它。