jfr*_*k53 10 javascript jquery dom alertify
我正在尝试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>
Run Code Online (Sandbox Code Playgroud)
当然,如果我aConf用JS 替换' confirm它的工作原理.那么为什么alertify不把它送回去呢?
Lev*_*evi 11
因为confirm是一个阻塞函数(在返回true/false之前没有javascript会运行),并且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
}
});
Run Code Online (Sandbox Code Playgroud)
对于您的代码示例,您可以尝试这样的事情:
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>
Run Code Online (Sandbox Code Playgroud)
编辑:更新为通用确认对话框,如果用户单击确定,则调用回调函数.
| 归档时间: |
|
| 查看次数: |
15266 次 |
| 最近记录: |