简体   繁体   中英

window.onbeforeunload alert in case of click of submit

I have the following code to handle to prompt user in case of unsaved form data but this code also showing alert in case of click of submit button

$(document).ready(function () {
    var _isDirty = false;
    $(":input").live("change", function () {
        _isDirty = true;
    });

    $(':[type=submit]').live('click', function () {
        _isDirty = false;            
    });
    window.onbeforeunload = function () {
        if (_isDirty) {            
            return 'You have made changes to data on this page.  If you navigate away from this page without first saving your data, the changes will be lost.';
        }
        else {           
            return null;
        }
    }
}

onbeforeunload has a quirk that if you return anything, even null, it will throw the alert. Try removing the else clause else {return null;} , and it should stop showing up.

Follow this post for your answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM