How do you handle a form change in jQuery?

后端 未结 13 2090
生来不讨喜
生来不讨喜 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条回答
  •  伪装坚强ぢ
    2020-11-28 21:32

    First, I'd add a hidden input to your form to track the state of the form. Then, I'd use this jQuery snippet to set the value of the hidden input when something on the form changes:

        $("form")
        .find("input")
        .change(function(){
            if ($("#hdnFormChanged").val() == "no")
            {
                $("#hdnFormChanged").val("yes");
            }
        });
    

    When your button is clicked, you can check the state of your hidden input:

    $("#Button").click(function(){
        if($("#hdnFormChanged").val() == "yes")
        {
            // handler code here...
        }
    });
    

提交回复
热议问题