簡體   English   中英

在Laravel Mix和Laravel Collective中無法識別preventDefault

[英]preventDefault not being recognized in Laravel Mix and Laravel Collective

我試圖阻止使用Laravel Collective制作的表單。 有趣的是,它只能工作一次,然后停止工作。 我正在與Laravel Mix一起工作。

這是我的代碼:

// Here's the form in my blade
{!! Form::open(['class' => 'confirm-delete','method' => 'DELETE', 'route' => ['users.destroy', $user->id], 'data-title' => __('Delete User') ]) !!}
    <button type="submit" class="dropdown-item" href="#" style="cursor: pointer"><i class="fas fa-trash-alt"></i> {{ __('Delete') }}</button>
{!! Form::close() !!}

//這是我的js代碼

$(".confirm-delete").submit(function (e) {
    alert("It doesn't alert this D:");
    var form = this;
    e.preventDefault();

    swal({
        title: form.data("title"),
        icon: "warning",
        buttons: [
            'No, cancel it!',
            'Yes, I am sure!'
        ],
        dangerMode: true,
     }).then(function (isConfirm) {
        if (isConfirm) {
        form.submit();
        }
    });
});

我嘗試了這些鏈接,但沒有一個起作用:

https://stackoverflow.com/a/54062772/3675186

https://laracasts.com/discuss/channels/laravel/how-to-continue-to-submit-a-form-after-preventing-it-with-jquery

使用JQuery-阻止表單提交

我正在使用Laravel 5.6。

終於明白了。 問題是我如何獲取數據屬性。

這是正確的代碼:

$(".confirm-delete").on('submit', function (e) {
    var form = this;
    e.preventDefault();

    swal({
        title: $(this).attr("data-title"),
        text: "You will not be able to recover this imaginary file!",
        icon: "warning",
        buttons: [
            'No, cancel it!',
            'Yes, I am sure!'
        ],
        dangerMode: true,
    }).then(function (isConfirm) {
        if (isConfirm) {
            form.submit();
        }
    });
});

希望它可以幫助某人:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM