ASP.NET, jQuery, dirty forms, and [removed]

前端 未结 5 2107
抹茶落季
抹茶落季 2020-12-16 17:07

In my ASP.NET web app, I\'m trying to create a universal way of warning users before navigating away from a form when they\'ve made changes, using jQuery. Pretty standard st

5条回答
  •  长情又很酷
    2020-12-16 17:18

    I came across this post while Googling for a solution for doing the same thing in MVC. This solution, adapted from Herb's above, seems to work well. Since there's nothing MVC-specific about this, it should work just as well for PHP, Classic ASP, or any other kind of application that uses HTML and JQuery.

    var isDirty = false;
    
    $(document).ready(function () {
        $(':input').bind("change select keydown", setDirty);
        $('form').submit(clearDirty);
    
        window.onbeforeunload = function (e) {
            var msg = "You have unsaved changes. "
            if (isDirty == true) {
                var e = e || window.event;
                if (e) { e.returnValue = msg; }
                return msg;
            }
        };
    });
    
    setDirty = function () { isDirty = true; }
    clearDirty = function () { isDirty = false; }
    

提交回复
热议问题