这就是我刚刚做到的方式:
$(document).ready(function(){ var action_is_post = false; $("form").submit(function () { action_is_post = true; }); window.onbeforeunload = confirmExit; function confirmExit() { if (!action_is_post) return 'You are trying to leave this page without saving the data back to the server.'; } });