Javascript Alertify返回确认

我正在尝试使用alertify.js作为我所有确认脚本的确认对话框。 但它只是不像普通的JS confirm那样工作。 在下面的代码中,我永远不会得到return true

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

当然,如果我用JS替换aConf confirm它有效。 那么为什么alertify不要把它送回去呢?

因为confirm是一个阻塞函数(没有javascript会运行,直到它返回true / false),并且alertify是非阻塞的(JS继续执行)。 Alertify不会立即返回true / false,而是可能会立即返回undefined,然后在用户单击“确定”或“取消”后调用回调函数。 该回调函数的返回值对您的示例没有影响,因为onclick代码已经完成运行(因为它是非阻塞的)。

假设您使用此: https : //github.com/fabien-d/alertify.js/

这是它实际上与回调函数一起使用的方式,而不是返回值:

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

对于您的代码示例,您可以尝试这样的事情:

 function performDelete ( a_element ) { // perform your delete here // a_element is the  tag that was clicked } function confirmAction ( a_element, message, action ) { alertify.confirm(message, function(e) { if (e) { // a_element is the  tag that was clicked if (action) { action(a_element); } } }); } Delete 

编辑:更新为通用确认对话框,如果用户单击确定,则调用回调函数。