开发者

Save form input with jQuery

开发者 https://www.devze.com 2023-03-08 03:38 出处:网络
I have a form with multiple fields on a page. If a user fills in the fields and then decides to leave the page he/she will loose it. I\'d like to add an alert using jQuery UI dialog to alert them abou

I have a form with multiple fields on a page. If a user fills in the fields and then decides to leave the page he/she will loose it. I'd like to add an alert using jQuery UI dialog to alert them about the data that has not been saved. I think I can get the buttons and dialog figured out. My question is:开发者_运维百科 how do I detect them leaving the page without submitting the form?


Here is a quick example - you need to be able to detect whether the form has been submitted as part of an onbeforeunload event.

var formSubmitted = false;
window.onbeforeunload = function () {
    if (!formSubmitted) {
        return confirm("Are you sure you want to leave the page without submitting the form"?);
    }
};

document.getElementById("myForm").onsubmit = function () {
    formSubmitted = true;
};


Have a look at the onbeforeunload event. However, keep in mind that some newer browsers like Firefox 4 do not allow you to customize the message that is displayed.


I'd recommend taking a look at the onbeforeunload event, here is some Mozilla developer connection documention to get you started: window.onbeforeunload

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号