over 3 years ago

用簡單概念實做,不把id直接塞在form的原因是,form有可能是search用的,也有可能是真的綁DB的form,大致上應該可以處理九成以上的需求了。

protected/views/index.php
<div class="form" id="sysform">
    some forms
  ...
  ...
</div>

<script>
    var unsaved = false;
    
    $('#sysform').live('change', function() {
        unsaved = true;
    });
    
    $('#sysform > input:button, input:submit').click(function() {
        unsaved = false;
    });
    
    $(window).on('beforeunload', function(e) {
        if(unsaved) {
            return '你有修改過,確定離開??';
        }
    });
</script>

參考資料:
http://api.jquery.com/unload/
http://hackab.it/2013/05/page-closing-confirm/
http://stackoverflow.com/questions/1551178/listen-for-change-events-in-forms-jquery
http://stackoverflow.com/questions/155739/detecting-unsaved-changes
http://stackoverflow.com/questions/11844256/alert-for-unsaved-changes-in-form
http://www.enkitec.com/apex/f?p=15300:30110:0::NO:::
http://www.railsmine.net/2013/11/javascript-alert-for-unsaved-changes-turbolinks-jquery.html

← windows使用tail指令 javascript debug →