【问题标题】:Example for using JQuery DirtyForms plugin使用 JQuery DirtyForms 插件的示例
【发布时间】:2012-06-18 22:40:26
【问题描述】:

我正在使用 jQuery DirtyForms 插件来防止用户在不保存的情况下无意中离开页面。我在对话框中遇到了一些问题。我有两个按钮:取消和不保存。

取消操作正常工作:对话框关闭,新页面未加载。但是,“不保存”操作不起作用。应该加载新页面,但没有发生。

我一定遗漏了一些明显的东西。谁能看到我做错了什么?

<script type="text/javascript">

    $(document).ready(function(){
            $('form').dirtyForms();
    });

    $('a').click(function(event){
            event.preventDefault();
            var targetUrl = $(this).attr("href");

            if($.DirtyForms.isDirty()){
                    $("#dialog-savechanges").dialog({
                            height: 250,
                            width: 500,
                            modal: true,
                            buttons: {
                                    Cancel: function() {
                                            $( this ).dialog( "close" );
                                            $.DirtyForms.choiceContinue = false;
                                            $.DirtyForms.choiceCommit(event);
                                    },
                                    "Don't Save": function() {
                                            $( this ).dialog( "close" );
                                            $.DirtyForms.choiceContinue = true;
                                            $.DirtyForms.choiceCommit(event);
                                    }
                            }
                    });
            }
    });

</script>

<div id="dialog-savechanges" title="Save Changes?">
    <p>You've made changes to this page. Do you want to leave this page without saving?</p>
</div>

谢谢!

-史蒂夫

【问题讨论】:

  • 请参阅this answer,了解实现保存更改按钮的正确方法。

标签: jquery jquery-plugins jquery-dirtyforms


【解决方案1】:

好的,我知道这很明显……希望这对其他人有帮助。

问题在于 preventDefault 函数:

event.preventDefault();

删除后,对话框的行为与预期相同。

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2016-11-11
    • 2010-12-26
    • 1970-01-01
    • 2013-12-05
    相关资源
    最近更新 更多