Javascript通过确认返回确认

编程入门 行业动态 更新时间:2024-10-09 01:23:35
本文介绍了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代码已经完成运行(因为它是非阻塞的)。

假设您正在使用这一点: github/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: github/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通过确认返回确认

本文发布于:2023-11-27 21:34:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!