删除beforeClose事件处理程序 - jQuery UI Dialog

Abi*_*tro 1 javascript jquery jquery-ui jquery-ui-dialog noty

我有一个jQuery UI对话框,里面有一个表单.我正在尝试实现一项功能,如果表单中的字段已被修改,我们将使用noty显示确认消息

现在,与javaScript确认框不同,noty不会停止脚本执行.所以,在对话beforeClose活动中,我 -

如果表单数据被修改然后返回false,则显示一个noty确认.如果表单数据尚未修改,则返回true .

一切都运作良好.现在我们问用户 -

表单中的数据已被修改.你确定要关闭吗?

如果他点击 - 我们只需关闭noty并保持对话框打开.但如果他单击,我们会尝试关闭对话框,beforeClose再次触发事件处理程序,然后我们再次进入循环.

我试着叫.offdiv调用close事件之前已被转换到一个对话框,但是这似乎并不在去除点击.

这是一个简单的伪代码来解释这个问题 -

DialogCloseEvent() {
    if data has been modified { 
        Show noty {
            // IMPORTANT - This is executed after return false.
            in noty user clicks NO - do not close dialog box {
                close noty and done
            } 
            in noty user clicks YES - close the dialog box {
                // This calls the DialogCloseEvent again.   
                call the close method of the dialog box.            
                close noty
            }
        }
        return false
    }   
    no it has not been modifed {
        // Closes the dialog without calling the event again
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*mer 5

扩展您的伪代码,您可以添加一个标志来强制关闭对话框:

var forceClose = false;

DialogCloseEvent() {
    if data has been modified and !forceClose  { 
        Show noty {
            // IMPORTANT - This is executed after return false.
            in noty user clicks NO - do not close dialog box {
                close noty and done
            } 
            in noty user clicks YES{ 

                forceClose = true;

                - close the dialog box {
                    // This calls the DialogCloseEvent again.   
                    call the close method of the dialog box.            
                    close noty
                }
            }
        }
        return false
    }   
    no it has not been modifed {
        // Closes the dialog without calling the event again
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

用代码更新

var forceClose = false;

$("#dialog").dialog({
    open: function (event, ui) {
        forceClose = false; //reset the flag each time
    },
    beforeClose: function (event, ui) {
        if(forceClose){
            return true;
        }

        //your dialog close event
        //set the flag to true when you want to close the jqueryui dialog

    }
});
Run Code Online (Sandbox Code Playgroud)