重写 JavascriptConfirm() 同时保留回调

发布于 2024-09-26 17:56:26 字数 882 浏览 3 评论 0原文


到目前为止,我想重写 jQuery 插件中的标准 js confirm() 函数。我已经弄清楚如何使用下面的简单功能布局来做到这一点。

function confirm(opts) {
   //code
}

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

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

openModal 函数将打开一个自定义模式窗口,其中包含消息和所需的信息按钮。这两个按钮都是 idsubmitcancelsubmit 返回 true,cancel 返回 false。但现在,我如何根据单击的按钮返回 true 或 false?

例如......

$('#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>
});

希望你明白我的意思。

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
}

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

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

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?

For example...

$('#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>
});

Hopefully you understand what I mean.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-10-03 17:56:26

这是可能的,但不完全像您所描述的那样。确认函数的典型使用方式是:

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

在上面的示例中,按钮按下同步返回到脚本。函数内的执行会暂停,直到按下按钮为止。

“模态对话框”只是 HTML 网页全部内容的覆盖层。对于浏览器来说,此类覆盖层中的按钮的工作方式与该页面中其他任何位置的按钮的工作方式相同。

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

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

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

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

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.

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.

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();
    }
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文