How do I know that a form input has changed?

后端 未结 5 454
温柔的废话
温柔的废话 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条回答
  •  萌比男神i
    2021-02-04 01:31

    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()

提交回复
热议问题