我应该在哪里放置SweetAlert代码以确认删除



这是我在

index.blade.php

上的代码
{{ Form :: open  (['method'=>'DELETE', 'route'=>['assistants.destroy', $value -> id , 'style'=> 'Display']])}}
                <button  type="submit" style="display: inline;" class="btn btn-danger btn-sm" ><i class=" glyphicon glyphicon-trash">
                    </i>
                </button>
                {{ Form::close() }}

假设您正在为项目使用Laravel 5.5 ,那么您可以做这样的事情:

向您的表格添加ID:

{!! Form::open(['method'=>'DELETE', 'route'=>['assistants.destroy', $value -> id , 'style'=> 'Display', 'id' => 'MyDeleteForm']]) !!}
<button  type="submit" style="display: inline;" class="btn btn-danger btn-sm">
    <i class=" glyphicon glyphicon-trash"></i>
</button>
{!! Form::close() !!}

然后您可以使用此脚本使用Ajax左右处理您的请求:

var MyDeleteForm = $('#MyDeleteForm');
MyDeleteForm.submit(function(e) {
    e.preventDefault();
    Swal.fire({ 
        title: 'Are you sure?', 
        text: "You won't be able to revert this!", 
        type: 'warning', 
        showCancelButton: true, 
        confirmButtonColor: '#3085d6', 
        cancelButtonColor: '#d33', 
        confirmButtonText: 'Yes, delete it!' 
    }).then((result) => { 
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,
            success: function(data) {
                Swal.fire( 'Deleted!', 'Your file has been deleted.', 'success' ) 
            },
            error: function(error) {
                console.log(error);
            }
        });
    });
});

我希望这对您有帮助。

最新更新