SimpleModal 关闭对话框前确认

发布于 2024-08-17 13:27:42 字数 471 浏览 7 评论 0原文

我正在使用 SimpleModal (http://www.ericmmartin.com/projects/simplemodal/ )并且我有一个显示在对话框中的表单。我想要做的是每次用户尝试关闭对话框(通过转义或单击关闭图标)时都能够出现确认,并询问他们是否真的想在不保存表单数据的情况下关闭它。我尝试了以下操作:

onClose: function (dialog) {
    if (confirm('Are you sure you want to close without saving?')) {
        $.modal.close();
    }
}

但它只触发一次。如果您点击取消,则稍后无法再次关闭,这是有道理的。有人有建议或解决方案吗?任何帮助将不胜感激。 :)

I'm using SimpleModal (http://www.ericmmartin.com/projects/simplemodal/) and I have a form that displays in a dialog. What I want to do is be able to have a confirmation come up each time that the user tries to close the dialog (either by escape or clicking on the close icon) and asks them if they really want to close it without saving the form data. I tried the following:

onClose: function (dialog) {
    if (confirm('Are you sure you want to close without saving?')) {
        $.modal.close();
    }
}

But it only triggers once. If you hit cancel then fails to close again later, which kind of makes sense. Anybody have a suggestion or solution? Any help would be greatly appreciated. :)

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

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

发布评论

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

评论(3

似狗非友 2024-08-24 13:27:42

我为您查看了 SimpleModal 的源代码,您想要做的事情无法用他们的代码完成。这就是为什么:

在调用自定义回调 onClose 之前,它会调用以下内容:

s.unbindEvents();

这实际上表示“无论您是否喜欢,此框都将关闭”。它与您可以取消的普通回调不同。

我建议使用 jQuery UI 对话框,您应该会发现通过使用它非常容易实现该功能他们的 beforeclose 回调。您只需使用:

beforeclose: function(){ 
    return confirm('Are you sure you want to close without saving?')
}

I looked at the source of SimpleModal for you and what you are wanting to do can't be done with their code. This is why:

Just prior to calling your custom callback onClose it calls this:

s.unbindEvents();

Which effectively says "This box is going to close whether you like it or not". It is not like a normal callback which you can cancel.

I would recommend instead using the jQuery UI Dialog, which you should find super easy to implement that functionality by using their beforeclose callback. You would simply use:

beforeclose: function(){ 
    return confirm('Are you sure you want to close without saving?')
}
疧_╮線 2024-08-24 13:27:42

我使用 jerone 正在尝试的方式来完成此工作,但也重新绑定事件:

onClose: function (dialog) {
    if (confirm('Are you sure you want to close without saving?')) {
        $.modal.close();
    }else{
        this.occb = false;
        this.bindEvents();
    }
}

需要更新此插件以支持关闭事件的取消。看起来它并没有真正被视为代码中的事件。我希望它的行为就像任何其他 js 事件一样。

I got this working using sort of what jerone was trying but also rebinding the events:

onClose: function (dialog) {
    if (confirm('Are you sure you want to close without saving?')) {
        $.modal.close();
    }else{
        this.occb = false;
        this.bindEvents();
    }
}

This plugin needs to be updated to support the cancel of the close event. It looks like its not really being thought of as a event in the code. I would like it to behave just like any other js event.

檐上三寸雪 2024-08-24 13:27:42

我还查看了源代码,当执行 onclose 事件时,会启用 occb 标志。

您可以尝试的(我没有尝试过)是在传递给 this 变量时覆盖此 occb

onClose: function (dialog) {
    if (confirm('Are you sure you want to close without saving?')) {
        $.modal.close();
    }else{
        this.occb = false;
    }
}

希望这会有所帮助。

I've also looked at the source and when the onclosed event is executed a occb flag is enabled.

What you can try (I haven't tried it) is to override this occb as it's passed to the this variable:

onClose: function (dialog) {
    if (confirm('Are you sure you want to close without saving?')) {
        $.modal.close();
    }else{
        this.occb = false;
    }
}

Hope this helps.

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