我应该把sweetalert的代码放在哪里确认删除?

问题描述 投票:0回答:1

这是我的代码 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 sweetalert2
1个回答
0
投票

假设您正在为项目使用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);
            }
        });
    });
});

我希望这能帮到您。

© www.soinside.com 2019 - 2024. All rights reserved.