2017-09-01 76 views
0
其他頁面

在我的應用程序,如果我因此未單擊保存,想從當前頁面導航應該問甜蜜警報are you sure want to leave?和兩個按鈕save and leave。我怎樣才能做到這一點。SweetAlert而導航到

目前我使用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 
         } 

        ); 
       }  
     }); 
+0

看看'preventDefault()'... – hallleron

回答

0

起初,你需要防止<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; 
    } 
}); 

希望幫助

+0

我試過這個..但它觸發甜蜜的警報,當頁面加載第一次 – Ash

+0

我真的在小提琴中檢查它,你的問題不會持續,是否有任何聲明類似到你的代碼中的某個地方? – masterpreenz

+0

No..This是唯一的地方。而我並沒有把它放在任何所需的function.is? – Ash

0

看看Event.preventDefault()或EP reventDefault()

該定位標記a將創建一個導航到其URL的事件,即使它是href=#