![](/img/trans.png)
[英]How to stop jquery function while navigating/reloading to other page?
[英]SweetAlert while navigating to other page
在我的應用程序中,如果我沒有單擊“保存”並想從當前頁面導航,則應詢問甜蜜提示are you sure want to leave?
和兩個按鈕save and leave
。 我怎樣才能做到這一點。
當前,我正在使用jquery,當我單擊任何錨標記時,它會顯示sweetalert框,但立即導航到其他頁面。
我的代碼是
jQuery(document).click(function(e) {
if (jQuery(e.target).is('a')) {
swal({
title: "Are you sure?",
text: "Want to continue without saving?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Leave",
closeOnConfirm: false,
html: false
}
);
}
});
首先,您需要防止<a>
元素的默認行為,因為您錯過了它,它仍然會重定向您的操作;其次,如果在swal
中單擊確認,則需要重定向用戶,例如:
jQuery(document).click(function(e)
{
if (jQuery(e.target).is('a'))
{
// Prevent default behavior
e.preventDefault();
swal({
title: "Are you sure?",
text: "Want to continue without saving?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Leave",
closeOnConfirm: false,
html: false
}, function (isConfirm) {
if (isConfirm)
{
// If user clicked confirm navigate away
window.location = jQuery(e.target).attr("href");
}
});
return false;
}
});
希望能有所幫助
看一下Event.preventDefault()或e.preventDefault()
該錨標記a
將創建一個事件以導航到其url,即使它的href=#
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.