简体   繁体   English

jQuery Dirrty表单将其手动清除

[英]jquery dirrty form set it clean manually

I am using jquery dirrty plugin to check the state of a form and prevent it from reloading if there are some unsaved changes. 我正在使用jQuery Dirrty插件来检查表单的状态,并在有一些未保存的更改时阻止它重新加载。

Link to jquery.dirrty github 链接到jquery.dirrty github

Flow: 流:

1) I have initialized jquery.dirty as follows 1)我已经初始化了jquery.dirty如下

$(function(){
    initializeDirtyForm();
})

/** dirty form initialization*/
function initializeDirtyForm(){
    $("#uAForm").dirrty().on("dirty", function(){
        $("#uAFormSubmit").removeAttr("disabled");
    }).on("clean", function(){
        $("#uAFormSubmit").attr("disabled", "disabled");
    });
}

2) There is a table on the page and when it click on the td cells it make an ajax call. 2)页面上有一个表格,当单击td单元格时,将进行ajax调用。

function ajaxCallEdit()
{
    var result = checkDirtyStatus();
        if(result === false) {
            return;
        }
    $.ajax({
        ....
        ....
        ....
        ....
        success:function(){
           initializeDirtyForm();
        }
    )}
}

/** check if the form is dirty */
function checkDirtyStatus(){
    dirtyStatus = $("#uAForm").dirrty("isDirty");
    if(dirtyStatus == true){
        if (confirm("Changes you made may not be saved. Do you still want to reload?")) {
            return true;
        }else{
            return false;
        }
    }
}

If the form is dirty, it works totally fine and show warning message. 如果表格脏了,则可以正常工作并显示警告消息。

ISSUE 问题

Here is the main issue, if I force it to reload, (this means it won't actually reload page, instead it would make an ajax call) it is still setting the status of the form as dirty. 这是主要问题,如果我强迫它重新加载(这意味着它实际上不会重新加载页面,而是会进行ajax调用),但它仍将表单的状态设置为脏。 I have tried by re-initializing the jquery form but still the form is flagged as dirty. 我已经尝试过通过重新初始化jquery表单来进行尝试,但是该表单仍被标记为脏表单。

To make it extra sure and actually what is the the status of the form, I checked the status and tried to console log on ajaxSuccess. 为了更加确定表单的实际状态,我检查了状态并尝试控制台ajaxSuccess的登录。

....

success: function(){
    initializeDirtyForm();
    var result = checkDirtyStatus();
    console.log(result);
}

...

However, consoling this result showing the value as undefined . 但是,使结果显示为undefined结果令人感到安慰。

I could not find any documentation related to the setting it manually and reinitalizing is not working as intented. 我找不到与手动设置有关的任何文档,并且重新初始化无法按预期进行。

So, if you are javascript wizard could you please check the jquery.dirrty.js file attached above and check if I could trigger following part from the js file or any other hacks that helps me to solve the problem. 因此,如果您是JavaScript向导,请检查上面附带的jquery.dirrty.js文件,并检查是否可以从js文件或任何其他可以帮助我解决问题的技巧中触发以下内容。

setClean: function(){
            this.isDirty = false;
            this.history[0] = this.history[1];
            this.history[1] = "clean";
        }

If you need any further details please let me know. 如果您需要更多详细信息,请告诉我。

try $("#uAForm").dirrty("setClean"); 尝试$(“#uAForm”)。dirrty(“ setClean”);

I just got into needing to do the same thing and that worked for me. 我只是需要做同样的事情而对我有用。

Cheers 干杯

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

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