简体   繁体   English

SimpleModal在关闭对话框之前确认

[英]SimpleModal confirm before closing dialog

I'm using SimpleModal ( http://www.ericmmartin.com/projects/simplemodal/ ) and I have a form that displays in a dialog. 我正在使用SimpleModal( http://www.ericmmartin.com/projects/simplemodal/ ),我有一个显示在对话框中的表单。 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. 任何帮助将不胜感激。 :) :)

I looked at the source of SimpleModal for you and what you are wanting to do can't be done with their code. 我为你查看了SimpleModal的源代码,你想要做的事情不能用他们的代码完成。 This is why: 这就是为什么:

Just prior to calling your custom callback onClose it calls this: 在调用自定义回调onClose之前,它调用:

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. 我建议改为使用jQuery UI Dialog ,你会发现使用它们的beforeclose回调非常容易实现这个功能。 You would simply use: 你只需使用:

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

I got this working using sort of what jerone was trying but also rebinding the events: 我使用jerone正在尝试的那种方式工作,但也重新绑定事件:

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. 我希望它的行为与任何其他js事件一样。

I've also looked at the source and when the onclosed event is executed a occb flag is enabled. 我还查看了源代码,当执行onclosed事件时,启用了occb标志。

What you can try (I haven't tried it) is to override this occb as it's passed to the this variable: 您可以尝试(我还没有尝试过)是覆盖此occb因为它传递给this变量:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM