Detecting data changes in forms using jQuery

后端 未结 10 2036
遇见更好的自我
遇见更好的自我 2020-12-05 13:27

I\'m using ASP.NET 2.0 with a Master Page, and I was wondering if anyone knew of a way to detect when the fields within a certain

or fieldset<
10条回答
  •  醉酒成梦
    2020-12-05 14:15

    For a form you could serialize the contents on load then compare serialization at a later time, e.g.:

    $(function(){
        var initdata=$('form').serialize();
        $('form').submit(function(e){
            e.preventDefault();
            var nowdata=$('form').serialize();
            if(initdata==nowdata) console.log('nothing changed'); else console.log('something changed');
            // save
            initdata=nowdata;
            $.post('settings.php',nowdata).done(function(){
                console.log('saved');
            });
        });
    });
    

    Note this requires form elements to have a name attribute.

提交回复
热议问题