我如何知道表单输入已更改?

发布于 2024-10-09 17:12:53 字数 180 浏览 2 评论 0原文

我有一个由 <% Ajax.BeginForm() {} %> 生成的表单,其中包含大量输入和 texareas。

当输入值发生变化时,我需要了解它并将输入和表单标记为“脏”。如果用户尝试在不保存的情况下离开页面,我会要求他或她确认放弃更改。

对于如何做到这一点有什么建议吗?

I have a form generated by <% Ajax.BeginForm() {} %> which contains a lot of inputs and texareas.

When an input value change, I need to know about it and mark the input and the form as "dirty". If the user tries to leave the page without saving, I will ask him or her to confirm abandoning changes.

Any suggestion to how this should be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾北清歌寒 2024-10-16 17:12:53

复活这个老问题是因为我想到了一个更简单/更好的方法。您可以序列化初始表单数据,存储它,然后稍后再次序列化并检查它是否已更改,而不是侦听各种输入上的事件,如下所示:

var originalFormData = $('form#formId').serialize();
function checkFormChanged() {
    if(originalFormData !== $('form#formId').serialize()) {
        //it's dirty!
    }
}

这里的另一个优点是,如果用户进行更改,然后恢复它,此检查将报告表单为干净的。

Resurrecting this old question because I thought of a simpler/better way. Instead of listening to events on the various inputs, you can serialize the initial form data, store it, and then serialize it again later and check if it's changed, like this:

var originalFormData = $('form#formId').serialize();
function checkFormChanged() {
    if(originalFormData !== $('form#formId').serialize()) {
        //it's dirty!
    }
}

One additional advantage here is that if the user makes a change and then reverts it, this check will report the form as clean.

〃温暖了心ぐ 2024-10-16 17:12:53

html 控件包含一个保存原始值的属性。您可以将该值与当前值进行比较,看看是否有任何变化。

function getHasChanges() {
    var hasChanges = false;

    $(":input:not(:button):not([type=hidden])").each(function () {
        if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue != this.value) {
            hasChanges = true;
            return false;             }
        else {
            if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) {
                hasChanges = true;
                return false;                 }
            else {
                if ((this.type == "select-one" || this.type == "select-multiple")) {
                    for (var x = 0; x < this.length; x++) {
                        if (this.options[x].selected != this.options[x].defaultSelected) {
                            hasChanges = true;
                            return false;
                        }
                    }
                }
            }
        }
    });

    return hasChanges;
}

function acceptChanges() {
    $(":input:not(:button):not([type=hidden])").each(function () {
        if (this.type == "text" || this.type == "textarea" || this.type == "hidden") {
            this.defaultValue = this.value;
        }
        if (this.type == "radio" || this.type == "checkbox") {
            this.defaultChecked = this.checked;
        }
        if (this.type == "select-one" || this.type == "select-multiple") {
            for (var x = 0; x < this.length; x++) {
                this.options[x].defaultSelected = this.options[x].selected
            }
        }
    });
}

The html controls include a property that holds the original value. You could compare this value with the current value to see if there have been any changes.

function getHasChanges() {
    var hasChanges = false;

    $(":input:not(:button):not([type=hidden])").each(function () {
        if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue != this.value) {
            hasChanges = true;
            return false;             }
        else {
            if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) {
                hasChanges = true;
                return false;                 }
            else {
                if ((this.type == "select-one" || this.type == "select-multiple")) {
                    for (var x = 0; x < this.length; x++) {
                        if (this.options[x].selected != this.options[x].defaultSelected) {
                            hasChanges = true;
                            return false;
                        }
                    }
                }
            }
        }
    });

    return hasChanges;
}

function acceptChanges() {
    $(":input:not(:button):not([type=hidden])").each(function () {
        if (this.type == "text" || this.type == "textarea" || this.type == "hidden") {
            this.defaultValue = this.value;
        }
        if (this.type == "radio" || this.type == "checkbox") {
            this.defaultChecked = this.checked;
        }
        if (this.type == "select-one" || this.type == "select-multiple") {
            for (var x = 0; x < this.length; x++) {
                this.options[x].defaultSelected = this.options[x].selected
            }
        }
    });
}
将军与妓 2024-10-16 17:12:53

来自 jQuery 文档:

//This applies to whole form
$('#formID').change(function() {
  alert('Form changed!');
});

您可以这样做来仅检查输入并在用户尝试退出而不保存更改时通知用户。

var inputsChanged = false;
$('#formID input').change(function() {
  inputsChanged = true;
});

$(window).unload(function() {
  if (inputsChanged === true) {
    alert('Would you like to save your edits before exiting?'); 
  }    
});

jQuery API .change()

From jQuery Docs:

//This applies to whole form
$('#formID').change(function() {
  alert('Form changed!');
});

And you could do like this to check only the inputs and have user notified, if they try to exit without saving changes.

var inputsChanged = false;
$('#formID input').change(function() {
  inputsChanged = true;
});

$(window).unload(function() {
  if (inputsChanged === true) {
    alert('Would you like to save your edits before exiting?'); 
  }    
});

jQuery API .change()

零度° 2024-10-16 17:12:53

我会用 jQuery 来做这件事。它会是这样的(只是一个草稿,您可能需要添加/更改一些代码):

$(document).ready(function() {  
    $('#formId:input').each(function(key) {
        $(this).change(function() {
            $(this).addClass('dirty');
        });
    });
});

然后,在发布之前,检查是否存在脏输入。您甚至可以使用 dirty css 类添加一些颜色。

编辑:拼写错误将“更改”修复为“更改”

I would do this with jQuery. It would be something like this (just a draft, you may need to add/change some code):

$(document).ready(function() {  
    $('#formId:input').each(function(key) {
        $(this).change(function() {
            $(this).addClass('dirty');
        });
    });
});

Then, before POSTing, check if there is a dirty input. You can even add some color by using the dirty css class.

EDIT: typo fixed 'changed' to 'change'

夜无邪 2024-10-16 17:12:53

您可以使用 jquery 插件 dirrty 来识别表单是否脏。

https://github.com/rubentd/dirrty

在 on("dirty") 事件中设置一些全局的变量为 true 来标识表单已更改。

处理 window.onbeforeunload$(window).unload() 事件并根据该变量值获取用户确认。

该插件的优点是您可以使用 'on("clean") 事件再次跟踪表单是否更改为原始值

You could use jquery plugin dirrty for identifying form dirty.

https://github.com/rubentd/dirrty

In on("dirty") event set some global variable to true to identify form has changed.

Handle window.onbeforeunload or $(window).unload() event and get confirmation from user based on that variable value.

The advantage of this plugin is you can track back if form is changed to original values again using 'on("clean") event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文