How do I know that a form input has changed?

后端 未结 5 405
温柔的废话
温柔的废话 2021-02-04 00:59

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

5条回答
  •  花落未央
    2021-02-04 01:44

    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.

提交回复
热议问题