2017-05-26 85 views
0

我是jQuery的新手,我使用here的確認框。但是,在我確認我在對話框中註銷之前,我正面臨一個問題,即當前頁面重定向回登錄頁面。在註銷前添加jQuery確認

下面是我的腳本:

$('a#logout').on('click', function() { 

         var isConfirmed = $.confirm({ 
          title: 'Logout Confirmation', 
          content: 'Are you sure you want to logout?', 
          buttons: { 
           confirm: function() { 
            // $.alert('Confirmed!'); 
            return true; 
           }, 
           cancel: function() { 
            // $.alert('Canceled!'); 
            return false; 
           }, 
          } 
         }); 

         if(isConfirmed == true){ 
          window.location.href="extra-login.html"; 
         } 

        }); 

這是我的HTML

<a href="extra-login.html" id="logout"> Log Out <i class="entypo-logout right"></i> </a>

任何幫助,將不勝感激。提前致謝!

+0

放重定向代碼在確認回調和刪除錨標記的href! –

+0

'href =「extra-login.html」'to'href' – Sankar

回答

1

問題是您的錨點元素的默認操作是採取用戶登錄頁面,這是不能阻止。

你可以撥打event.preventDefault()來做到這一點。

此外,confirm插件看起來像一個非阻塞插件,因此您需要將重定向代碼移動到成功處理程序。

$('a#logout').on('click', function(e) { 
    e.preventDefault(); 

    var isConfirmed = $.confirm({ 
    title: 'Logout Confirmation', 
    content: 'Are you sure you want to logout?', 
    buttons: { 
     confirm: function() { 
     window.location.href = "extra-login.html"; 
     }, 
     cancel: function() {}, 
    } 
    }); 
}); 
0

我猜對話沒有阻塞(我不知道任何在JS中)。你應該只是把你的成功代碼的回調確認按鈕,就像這樣:

$('a#logout').on('click', function() { 

         $.confirm({ 
          title: 'Logout Confirmation', 
          content: 'Are you sure you want to logout?', 
          buttons: { 
           confirm: function() { 
            // $.alert('Confirmed!'); 
            //I'm guessing this function is called when the button is clicked. 
            window.location.href="extra-login.html"; 
            return true; 
           }, 
           cancel: function() { 
            // $.alert('Canceled!'); 
            return false; 
           }, 
          } 
         }); 

        }); 

但你的代碼被立即重定向的主要原因是在你的鏈接href標籤。你基本上有一個到另一個頁面的鏈接,它也試圖運行一些JS,但是因爲它是在JS甚至可以運行之前重定向的鏈接。做到這一點,而不是:

<a href="#" id="logout"> 
    Log Out <i class="entypo-logout right"></i> 
</a> 
0

我新,太 我希望我的建議是正確的,但你爲什麼不把HREF在確認功能。

$('a#logout').on('click', function (event) { 
    var isConfirmed = $.confirm({ 
         title: 'Logout Confirmation', 
         content: 'Are you sure you want to logout?', 
         buttons: { 
          confirm: function() { 
           window.location.href="extra-login.html"; 
          }, 
          cancel: function() { 
           event.preventDefault(); 
          }, 
         } 
        }); 

}); 
0

你爲什麼不使用這樣的 -

$('a#logout').on('click', function(){ 
    if($(this).confirm({title: 'Logout Confirmation', content: 'Are you sure you want to logout?'})){ 
    window.location.href="extra-login.html"; 
}else{ 
    return false; 
} 
});