Activating OnBeforeUnload ONLY when field values have changed

后端 未结 7 2109
暗喜
暗喜 2020-12-31 01:27

What I\'m trying to achieve is to Warn the user of unsaved changes if he/she tries to close a page or navigate away from it without saving first.

I\'ve managed to g

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 02:22

    We just use Window.onbeforeunload as our "changed" flag. Here's what we're doing, (using lowpro):

    Event.addBehavior({
      "input[type=radio]:change,input[type=text]:change,input[type=checkbox]:change,select:change": function(ev) {
           window.onbeforeunload = confirmLeave;
      }
      ".button.submit-button:click": function(ev) {
           window.onbeforeunload = null;
      },
    });
    
    
    function confirmLeave(){
        return "Changes to this form have not been saved. If you leave, your changes will be lost."  
    }
    

提交回复
热议问题