在保留回调的同时覆盖 Javascript Confirm() [英] Overriding Javascript Confirm() while preserving the callback

查看:19
本文介绍了在保留回调的同时覆盖 Javascript Confirm()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


就此而言,我想覆盖 jQuery 插件中的标准 js confirm() 函数.我已经弄清楚如何使用下面的简单功能布局来做到这一点.


To the point, I want to override the standard js confirm() function within a jQuery plugin. I have figured out how to do it with the simple function layout below.

function confirm(opts) {
   //code
}

现在,我想做的是在上面的 confirm 函数中调用另一个函数,就像这样......

Now, what I want to do is call another function within the confirm function above, like so...

function confirm(opts) {
   openModal(opts);
}

openModal 函数将打开一个带有消息和所需按钮的自定义模式窗口.按钮都是 ,带有 idsubmitcancel.submit 返回真,cancel 返回假.但是现在,如何根据单击的按钮返回 true 或 false?

The openModal function will open a custom modal window with the message and required buttons. The buttons are both <span> with the id of either submit or cancel. submit returns true and cancel returns false. But now, how do I return either true or false based on the button clicked?

例如...

$('#openModal').live('click', function() {
   var opts = { title:'Modal Title', message:'This is a modal!' };
   var resp = confirm(opts);
   if(resp) 
      // user clicked <span id="submit"></span>
   else 
      // user clicked <span id="cancel"></span>
});

希望你明白我的意思.

推荐答案

可能的,但与您描述的不完全一样.确认函数的典型使用方式是:

This is possible but not exactly as you describe. The typical way the confirm function is used is:

if(confirm("Are you sure you want to submit this form?")) {
    form.submit();
}

在上面的例子中,按钮按下同步返回到脚本.执行在函数内暂停,直到按下按钮.

In the above example, the button press is returned synchronously to the script. Execution is paused within the function until a button is pressed.

模态对话框"只是 HTML 网页全部内容的叠加.对于浏览器来说,这种叠加层中的按钮的工作方式与该页面中其他任何位置的按钮的工作方式相同.

A "modal dialog" is just an overlay over the entire contents of an HTML web page. To the browser, buttons within such an overlay work the same way as buttons anywhere else within that page do.

由于 JavaScript 异步处理此类按钮点击(即使用回调函数处理事件)而不是同步处理,因此您尝试的方法将不起作用.

Since JavaScript handles such button clicks asynchronously (i.e. using callback functions to handle events) rather than synchronously, the approach you are attempting will not work.

您需要将使用确认函数的所有代码部分更改为如下所示:

You would need to change all parts of the code that use the confirm function to look like this:

confirm("Are you sure you want to submit this form?", function(result) {
    if(result) {
        form.submit();
    }
});

这就像使用 jQuery 本身注册事件处理程序一样.脚本立即继续执行,跳过匿名函数中的所有代码.稍后,浏览器将通过您在确认函数中注册的 JavaScript 事件处理程序间接调用该函数,其目的是根据需要使用 truefalse 调用回调函数.

This would work just as registering an event handler with jQuery itself does. Script execution continues immediately, skipping past all the code in the anonymous function. Later, the browser will call that function indirectly through a JavaScript event handler that you register within the confirm function, its purpose to call the callback function with either true or false as appropriate.

这篇关于在保留回调的同时覆盖 Javascript Confirm()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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