使用sweetalert库。这是我的表格:

<form id="delete-currency" style="display:inline;" method="POST" action="/admin/currency/7">
   <input type="hidden" name="_token" value="sIgpTKlPJ4Z3Co4daRwGpZ8rz10TCM6Ynre8sdsdsd">
   <input type="hidden" name="_method" value="DELETE">
   <button type="button" class="btn btn-danger btn-delete"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
</form>


7-是商品ID。

然后我正在使用确认窗口(sweetalert):

<script type="text/javascript">
    $('.btn-delete').click(function() {
        swal({
            title: "Are you sure?",
            text: "blablabla!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, i am sure",
            closeOnConfirm: false,
            cancelButtonText: "Cancel"
        },
        function(isConfirm) {
            $('#delete-currency').submit();
        });
    });

</script>


我有一个资源控制器,该控制器接受带有货币ID的delete方法。但是,如果我将Sweetalert与jquery submit()一起使用,它将始终发送“ 1”,而不是实际的$currency->id

我的控制器:

public function destroy($id)
{
    dd($id);
    $currency = Currency::findOrFail($id);
    $currency->delete();
    alert()->success('asdasdsadad', 'Success')->persistent('close');
    return redirect('/admin/currency');
}


dd($ id)始终显示“ 1”。

route.php:

Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function() {
    Route::resource('currency', 'CurrencyController');
});


怎么了?

最佳答案

尝试这个。

 $('.btn-delete').on('click',function(e){
    e.preventDefault();
    var form = $(this).parents('form');
swal({
    title: "Are you sure?",
     text: "blablabla!",
     type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    closeOnConfirm: false
}, function(isConfirm){
    if (isConfirm) form.submit();
});


})

我认为这将为您提供正确的价值。

希望能帮助到你

关于javascript - Sweetalert软件包和jQuery Submit()无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41220914/

10-13 09:03