Javascript通过确认返回确认 [英] Javascript Alertify with return from confirm

查看:225
本文介绍了Javascript通过确认返回确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 alertify.js 作为所有确认脚本的确认对话框。但它只是不像常规JS 确认那样工作。在下面的代码中,我从未得到一个 return true

  function aConf mes){
alertify.confirm(mes,function(e){
return e;
});
}

< a href =#onclick =if(aConf(\A你确定要删除这个?\')){function(); } return false;> Delete< / a>

当然,如果我用JS替换 aConf '确认它有效。那么为什么 alertify 不会发回我的结果?

解决方案

因为确认是一个阻止功能(没有javascript将运行,直到它返回true / false),并且alertify是非阻塞(JS保持执行)。 Alertify不会立即返回一个true / false,而是可能立即返回undefined,然后在用户单击OK或Cancel之后调用回调函数。该回调函数的返回值在您的示例中没有影响,因为onclick代码已经完成运行(因为它是非阻塞的)。



假设您正在使用这一点: https://github.com/fabien-d/alertify.js/



这是它实际上与回调函数的工作方式,而不是返回值:

 code> alertify.confirm(message,function(e){
if(e){
// click click OK
} else {
//点击取消
}
});

对于您的代码示例,您可以尝试以下操作:

  function performDelete(a_element){
//在这里执行你的删除
// a_element是< a>标签被点击
}

函数confirmAction(a_element,message,action){
alertify.confirm(message,function(e){
if(e) {
// a_element是被点击的< a>标签
if(action){
action(a_element);
}
}
});
}

< a href =#onclick =confirmAction(this,'你确定要删除吗?',performDelete); return false;> Delete< ; / A>

编辑:更新为一个通用的确认对话框,如果用户点击ok就调用回调函数。 / p>

I'm trying to use alertify.js as a confirmation dialog for all my confirm scripts. But it just isn't working like regular JS confirm does. In the code below I never get a return true

function aConf ( mes ) {
    alertify.confirm( mes, function (e) {
        return e;
    });
}

<a href="#" onclick="if(aConf(\'Are you sure you wish to remove this?\')) { function(); } return false;">Delete</a>

Of course if I replace aConf with JS' confirm it works. So why is alertify not sending me back it's outcome?

解决方案

Because confirm is a blocking function (no javascript will run until it returns true/false), and alertify is non-blocking (JS keeps executing). Alertify does not immediately return a true/false, but instead, it probably returns undefined immediately, then it calls a callback function later, after the user clicks OK or Cancel. The return value from that callback function has no effect in your example, because the onclick code has already finished running (because it is non-blocking).

Assuming you are using this: https://github.com/fabien-d/alertify.js/

This is how it actually works with a callback function, not a return value:

alertify.confirm( message, function (e) {
    if (e) {
        //after clicking OK
    } else {
        //after clicking Cancel
    }
});

For your code sample, you might try something like this:

function performDelete ( a_element ) {
    // perform your delete here
    // a_element is the <a> tag that was clicked
}

function confirmAction ( a_element, message, action ) {
    alertify.confirm(message, function(e) {
        if (e) {
            // a_element is the <a> tag that was clicked
            if (action) {
                action(a_element);
            }
        }
    });
}

<a href="#" onclick="confirmAction(this, 'Are you sure you wish to remove this?', performDelete); return false;">Delete</a>

EDIT: updated to be a generic confirm dialog that calls a callback function if the user clicks ok.

这篇关于Javascript通过确认返回确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆