Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除beforeClose事件处理程序-jQuery UI对话框_Javascript_Jquery_Jquery Ui_Jquery Ui Dialog_Noty - Fatal编程技术网

Javascript 删除beforeClose事件处理程序-jQuery UI对话框

Javascript 删除beforeClose事件处理程序-jQuery UI对话框,javascript,jquery,jquery-ui,jquery-ui-dialog,noty,Javascript,Jquery,Jquery Ui,Jquery Ui Dialog,Noty,我有一个jQueryUI对话框,里面有一个表单。我正在尝试实现一个功能,如果表单中的字段被修改,我们将使用 现在,与javaScript确认框不同,noty不会停止脚本执行。因此,在对话框beforeClose事件中,我- 如果表单数据被修改,则显示noty确认,然后返回false。 如果表单数据未修改,只需返回true即可 一切进展顺利。现在我们询问用户- 表单中的数据已被修改。您确定要关闭吗 如果他单击“否”,我们只需关闭noty并保持对话框打开即可。 但是如果他单击yes,我们将尝试关闭对

我有一个jQueryUI对话框,里面有一个表单。我正在尝试实现一个功能,如果表单中的字段被修改,我们将使用

现在,与javaScript确认框不同,noty不会停止脚本执行。因此,在对话框
beforeClose
事件中,我-

如果表单数据被修改,则显示noty确认,然后返回false。 如果表单数据未修改,只需返回true即可

一切进展顺利。现在我们询问用户-

表单中的数据已被修改。您确定要关闭吗

如果他单击“否”,我们只需关闭noty并保持对话框打开即可。 但是如果他单击yes,我们将尝试关闭对话框,这将再次触发
beforeClose
事件处理程序,并再次进入循环

我尝试在调用关闭事件之前已转换为对话框的
div上调用
.off
,但这似乎并没有删除单击

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

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

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

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;
    }
}
用代码更新

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

    }
});

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

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;
    }
}
用代码更新

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

    }
});

您可以使用对话框小部件的“option”方法更改或删除beforeClose事件处理程序

因此,当用户单击“是”时,您可以通过执行以下命令来关闭对话框:

$('#myDialog')
  .dialog('option', 'beforeClose', function() {})
  .dialog('close'); 
这里有一把小提琴,展示了它是如何工作的:


关于“option”方法的Jquery UI文档:

您可以使用对话框小部件的“option”方法更改或删除beforeClose事件处理程序

因此,当用户单击“是”时,您可以通过执行以下命令来关闭对话框:

$('#myDialog')
  .dialog('option', 'beforeClose', function() {})
  .dialog('close'); 
这里有一把小提琴,展示了它是如何工作的:


关于“选项”方法的Jquery UI文档:

根据Jquery UI对话框的
关闭
API的代码,在不触发事件的情况下,无法强制关闭对话框。理想情况下,应该有一个只执行功能而不触发事件的API。我尝试了一种方法来实现这一点,现在就复制这个方法。作为API本身的补充的最佳方式。所以这个变化是这样的

// code from existing
if(!forceClose && this._trigger("beforeClose") === false){
    return;
}
// continue with existing
在这里也有一把小提琴

现在将向jQueryUI提交特性/拉取请求。完成后将更新详细信息。同时,让我知道这是否有帮助

更新

Jquery用户界面票据-


Pull request-

根据Jquery UI对话框的
关闭
API的代码,无法在不触发事件的情况下强制关闭对话框。理想情况下,应该有一个只执行功能而不触发事件的API。我尝试了一种方法来实现这一点,现在就复制这个方法。作为API本身的补充的最佳方式。所以这个变化是这样的

// code from existing
if(!forceClose && this._trigger("beforeClose") === false){
    return;
}
// continue with existing
在这里也有一把小提琴

现在将向jQueryUI提交特性/拉取请求。完成后将更新详细信息。同时,让我知道这是否有帮助

更新

Jquery用户界面票据-


拉取请求-

示例1
根据:


在这两个示例中,将内部jqueryui确认对话框代码替换为noty。它允许您创建带有回调的操作按钮,以便代码类似。

示例1
根据:


在这两个示例中,将内部jqueryui确认对话框代码替换为noty。它允许您创建带有回调的操作按钮,以便代码类似。

这是一个问题,因为我们无法想出其他方法来关闭对话框,而不触发关闭事件。由于此noty对话框不会像confirm()一样阻止执行,因此我认为此方法优于取消绑定/绑定事件处理程序。但是在关闭noty之前可以解除beforeClose处理程序的绑定……这种方法很好,我已经尝试过了,但是我不确定如何使用
jqueryui
对话框来实现它。总之,upvote是为您准备的。感谢upvote,我添加了一些代码来帮助您更清楚地了解它。这是一个问题,因为我们无法想出一种在不触发关闭事件的情况下关闭对话框的替代方法。由于此noty对话框不会像confirm()那样阻止执行,因此我认为这种方法比取消绑定/绑定事件处理程序要好。但是在关闭noty之前可以解除beforeClose处理程序的绑定……这种方法很好,我已经尝试过了,但是我不确定如何使用
jqueryui
对话框来实现它。不管怎样,向上投票支持你。谢谢你的向上投票,我已经添加了一些代码来帮助你把它弄清楚。