Warn User When Navigating from Page with Unsaved Changes

前端 未结 1 1316
心在旅途
心在旅途 2020-12-20 04:10

I\'m creating an ASP.NET website and I want to implement logic to warn the user when they are navigating away from a page they\'ve edited.

I found quite a bit of inf

相关标签:
1条回答
  • 2020-12-20 04:54

    Your input elements probably do not exist when the code is executed. Try using the .live function to detect changes on all input elements, or wrap your code in a $(document).ready() handler.

    Example:

    $('input').live("change", function () {
        window.onbeforeunload = function () { return "Your changes have not been saved?" };
    });
    

    or

    $(document).ready(function () {
       $('input').change(function () {
          window.onbeforeunload = function () { return "Your changes have not been saved?" };
      });
    });
    
    0 讨论(0)
提交回复
热议问题