【问题标题】:preventDefault not being recognized in Laravel Mix and Laravel CollectivepreventDefault 在 Laravel Mix 和 Laravel Collective 中未被识别
【发布时间】:2019-07-17 04:15:43
【问题描述】:

我试图阻止默认使用 Laravel Collective 制作的表单。有趣的是它工作一次然后停止工作。我正在使用 Laravel Mix 处理我的 js。

这是我的代码:

// 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

Using JQuery - preventing form from submitting

我正在使用 Laravel 5.6。

【问题讨论】:

    标签: javascript jquery laravel webpack laravel-mix


    【解决方案1】:

    终于明白了。问题是我是如何获取数据属性的。

    这是正确的代码:

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

    希望它可以帮助某人:)

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 2017-03-05
      • 2018-09-17
      • 1970-01-01
      • 2019-06-15
      • 2019-05-30
      • 2019-02-13
      • 2020-11-16
      相关资源
      最近更新 更多