这已经被问过了,我知道,但我不明白为什么这对我不起作用。
尝试检测表单更改,如果用户尝试离开,我希望他们确认他们将丢失更改...
var dirty = false;
$("form#add_item :input").change(function() {
dirty = true;
});
$("window").on('beforeunload', function() {
if (dirty) {
return 'You have unsaved changes! If you leave this page, your changes will be lost.';
}
});</p>