【问题标题】:Confirm Before Deleting with Sweet Alert Laravel 5.5使用 Sweet Alert Laravel 5.5 在删除前确认
【发布时间】:2017-10-26 09:31:31
【问题描述】:

我希望甜蜜的警报框在不删除的情况下熄灭。

但是没有找到 ajax 的 id

路线

Route::get('room/delete/{id}', 'RoomList@destroy')->name('roomdelete');

查看文件rooms-list.blade.php

@foreach($rooms as $room)
<tr>
    <td>{!! $room->room_id !!}</td>
    <td>{!! $room->hotel_id !!}</td>
    <td>{!! $room->room_name !!}</td>
    <td>
        <a href="" class="button" data-id="{!! $room->room_id !!}"> <i class="fa fa-close text-danger"></i> </a></td>
</tr>

@endforeach

SweetAlert上面的Js代码

<script>
$(document).on('click', '.button', function (e) {
    e.preventDefault();
    var id = $(this).data('id');
    swal({
            title: "Are you sure!",
            type: "error",
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes!",
            showCancelButton: true,
        },
        function() {
            $.ajax({
                type: "POST",
                url: "{{url('room/delete')}}",
                data: {id:id},
                success: function (data) {

                    }
            });
    });
});


</script>

控制器文件RoomList.php

public function destroy($id) {

        $rooms = Room::find($id);
        $rooms->delete();
        return redirect()->back()->with('deleted', 'Delete Success!');

    }

点击删除按钮不起作用

最好的问候

【问题讨论】:

  • 检查我的答案:)

标签: laravel crud sweetalert laravel-5.5


【解决方案1】:

首先,你有一个GET 路由并且你在这里发送一个POST 请求,所以首先将你的ajax 方法更改为GET,如下所示:

<script>
$(document).on('click', '.button', function (e) {
    e.preventDefault();
    var id = $(this).data('id');
    swal({
            title: "Are you sure!",
            type: "error",
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes!",
            showCancelButton: true,
        },
        function() {
            $.ajax({
                type: "GET",
                url: "{{url('room/delete/')}}+id", // since your route has /{id} 
                data: {id:id},
                success: function (data) {

                    }
            });
    });
});


</script>

我也不确定$(document).on('click', '.button', function (e)我猜应该是这样的:

$('.button').on('click', '.button', function (e)

如果您仍有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    相关资源
    最近更新 更多