3

我是alertify.js的新手。我想做的是,当用户点击链接时,浏览器应该显示确认框。如果用户点击然后转到下一页。如果用户单击取消,则留在同一页面上。是否可以使用alertify.js做到这一点?

这是我的 HTML 代码。

<a href="index.php" class="alert">Logout</a>

这是我的 JavaScript 代码。

$(".alert").on('click', function(){
    alertify.confirm("Are you sure?", function (e) {
        if (e) {
            return true;
        } else {
            return false;
        }
    });
});

但问题是,每当我点击链接时,我都会在点击确认之前转到 index.php 页面。

4

2 回答 2

8

问题是不可能让代码等待。因此,您需要做的是取消原始点击,然后调用代码导航到新页面。

$(".alert").on('click', function(e){
    e.preventDefault();
    var href = this.href;
    alertify.confirm("Are you sure?", function (e) {
        if (e) {
            window.location.href = href;
        }
    });
});
于 2013-05-14T14:12:53.800 回答
-1
$(".alert").on('click', function(){
    e.preventDefault();
    alertify.confirm("Are you sure?", function (e) {
        if (e) {
            //window.location...redirect
        } else {
            //do something
        }
    });
});

您不需要返回 true 或 false ...您已经在 if 语句中检查它

于 2013-05-14T14:12:11.703 回答