2017-09-01 74 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=#