退出确认消息不起作用laraver-8



我注意到,当我点击注销按钮时,我正试图创建一条注销确认消息为此,我对我的navebar.blade.php 进行了此操作

<a class="dropdown-item" id="logout" href="{{ route('admin.logout') }}">
{{ __('Logout') }}
</a>

这是给我的管理员的。blade.php,但不工作,但它应该工作

<script>
$(docoment).on("click", "#logout", function(e) {
e.preventDefault();
var link = $(this).attr("href");
swal({
title: "Are you Want to logout?",
text: "",
icon: "warning",
buttons: true,
dragerMode: true,
})
.then((willDelete) => {
if (willDelete) {
window.location.href = link;
} else {
swal("Not Logout");
}
});
});
</script>

这里的<a>标记不等待click函数执行。它将重定向到href中的链接。因此,按如下方式更改函数。

<a class="dropdown-item" id="logout" data-link="{{ route('admin.logout') }}">
{{ __('Logout') }}
</a>
<script>
$(docoment).on("click", "#logout", function(e) {
e.preventDefault();
var link = $(this).attr("data-link");
swal({
title: "Are you Want to logout?",
text: "",
icon: "warning",
buttons: true,
dragerMode: true,
})
.then((willDelete) => {
if (willDelete) {
window.location.href = link;
} else {
swal("Not Logout");
}
});
});
</script>

如果您使用的是jQuery>=1.4.3,那么你也可以得到类似的数据属性

var link = $(this).data("link");

我更喜欢创建一个注销表单。不建议使用Get方法注销。

<form action="{{ route('logout') }}" method="post" id="logoutorm">
@csrf
<button type="submit" class="dropdown-item">
<i class="far fa-fw fa-arrow-alt-circle-left mr-1"></i> {{ __('Sign Out') }}
</button>
</form>

要通过甜蜜提醒提交此表单,您可以执行以下操作:

<script>
$(document).on("submit", "#logoutorm", function(event) {
event.preventDefault();
let form = $(this);
swal({
title: "Are you Want to logout?",
text: "",
icon: "warning",
buttons: true,
dragerMode: true,
})
.then((willDelete) => {
if (willDelete) {
form.submit();
} else {
swal("Logout Canceled");
}
});
});

希望这对你有用:(

最新更新