How do you handle a form change in jQuery?

后端 未结 13 2032
生来不讨喜
生来不讨喜 2020-11-28 21:18

In jQuery, is there a simple way to test if ANY of a form\'s elements have changed?

EDIT: I should have added that I only need to check on a c

13条回答
  •  -上瘾入骨i
    2020-11-28 21:23

    If you want to check if the form data, as it is going to be sent to the server, have changed, you can serialize the form data on page load and compare it to the current form data:

    $(function() {
    
        var form_original_data = $("#myform").serialize(); 
    
        $("#mybutton").click(function() {
            if ($("#myform").serialize() != form_original_data) {
                // Something changed
            }
        });
    
    });
    

提交回复
热议问题