简体   繁体   English

在Laravel Mix和Laravel Collective中无法识别preventDefault

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

I'm trying to preventDefault a form made with Laravel Collective. 我试图阻止使用Laravel Collective制作的表单。 The funny thing is that it works once and then it stops working. 有趣的是,它只能工作一次,然后停止工作。 I'm working my js with Laravel Mix. 我正在与Laravel Mix一起工作。

Here's my code: 这是我的代码:

// 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() !!}

// Here's my code in my js //这是我的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();
        }
    });
});

I have tried with these links but none have worked: 我尝试了这些链接,但没有一个起作用:

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

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

Using JQuery - preventing form from submitting 使用JQuery-阻止表单提交

I'm working with Laravel 5.6. 我正在使用Laravel 5.6。

Finally got it. 终于明白了。 The problem was how I was getting the data attributes. 问题是我如何获取数据属性。

Here's the correct code: 这是正确的代码:

$(".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();
        }
    });
});

Hope it helps someone :) 希望它可以帮助某人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM