简体   繁体   中英

How add event listener to window only if form has been changed

As many forms, users can accidentally click on some links when filling the fields.

But if they not fill anyone I don't need to prevent your unload.

How add event onbeforeunload to the window object only if form has been changed?

There is another way to do this , if you have jQuery , you can block all the link's (a) click , if user didn't fill anything .

function fillCheck(){
    return /* filled? (true or false) */;
}

$('body').on('click','a',function(){
    isFill =  fillCheck();
    if(!isFill){
        return false;
    }
})

Bind the event first and check if the form has changed.

Sample Code:

 window.onbeforeunload = function(e) { var formChanged = $(".name").val(); // This is example. Make any boolean value. if (formChanged) { return 'Dialog text here.'; } };

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