我正试图阻止使用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
Using JQuery - preventing form from submitting
我正在使用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();
}
});
});
希望它可以帮助别人:)