2017-05-26 47 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; 
} 
});