我正在尝试使用alertify.js作为我所有确认脚本的确认对话框。但它并不像普通的JS confirm那样工作。在下面的代码中,我从未得到过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>当然,如果我用JS的confirm替换aConf,它就可以工作。那么为什么alertify不把我送回它的结果呢?
发布于 2013-01-19 06:28:58
因为confirm是一个阻塞函数(在返回true/false之前不会运行javascript ),而alertify是非阻塞函数(JS一直在执行)。Alertify不会立即返回true/false,而是可能会立即返回undefined,然后在用户单击OK或Cancel之后调用回调函数。该回调函数的返回值在示例中不起作用,因为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 <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:更新为一个通用的确认对话框,如果用户单击ok,它将调用一个回调函数。
https://stackoverflow.com/questions/14408627
复制相似问题