To check the form input value in a text registration

Asked 2 years ago, Updated 2 years ago, 35 views

If the user tries to leave the page while writing, I want to activate beforeunload.

I'm not sure how to check whether the form has input values or not.

$("#form").on("change",function(){
    if (if the form has a value)
        $(window).on("beforeunload", function (){
            If you leave the "return" page, the contents you are creating will not be saved.";            
        });
    } } else {
        $(window).off("beforeunload");
    }
});

If there is a value in the form above, how should I check the part?

jquery

2022-09-22 16:56

1 Answers

When it's changed, check the dirty value and let me know if there's a dirty value when it's beforeunloading. It may not work if you bind it every time you change it, and I think it would be better to look at the history of the change than to check the value of the form.

$(document).ready(function() {
    formChanged = false; 
    window.onbeforeunload = checkDirty;
});

function checkDirty() {
    if (formChanged) {
        If you leave the "return" page, the contents you are creating will not be saved."; 
    }
}

$("#form").change(function() {
    formChanged = true;
});


2022-09-22 16:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.