简体   繁体   English

如果用户更改输入但未保存,则启动确认对话框

[英]launch confirm dialog if user changes input without saving

I need to implement in JS a warning if a user changes an input field in a form without saving. 如果用户在不保存的情况下更改了表单中的输入字段,则需要在JS中实施警告。 The expected behavior is that I make a change into the input and if I try to close the window the confirm dialog appears. 预期的行为是我对输入进行了更改,并且如果我尝试关闭窗口,则会出现确认对话框。

However, what actually happens is when I close the window, the window closes and the dialog does not appear. 但是,实际上发生的是当我关闭窗口,窗口关闭并且没有出现对话框时。

I'm referencing the following resources: 我引用了以下资源:

Warn user before leaving web page with unsaved changes 在离开网页之前进行未保存的更改之前警告用户

JavaScript before leaving the page 离开页面前的JavaScript

This is what I have currently: 这是我目前拥有的:

var formInputChanged = false;
$(function() {
    // activate modal is field is edited
    $(".account-settings-form input").on("input", function() {
        window.formInputChanged = true;
    });
    $("input[value='Cancel']").click(function() {
        window.formInputChanged = false;
    });

    window.addEventListener('beforeunload', (event) => {
        if (window.formInputChanged) {
            confirm("You have unsaved changes");
        }
    });
 });

I have that variable set globally so as far a I understand, should be fully accessible and I'm assigning it to the window in the functions. 据我所知,我已经全局设置了该变量,应该可以完全访问该变量,并将其分配给函数中的窗口。

I can see in the console.log() that formInputChanged is the correct value. 我可以在console.log()中看到formInputChanged是正确的值。

https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event https://developer.mozilla.org/zh-CN/docs/Web/API/Window/beforeunload_event

This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page. 此事件使网页能够触发一个确认对话框,询问用户是否真的要离开该页面。 If the user confirms, the browser navigates to the new page, otherwise it cancels the navigation. 如果用户确认,浏览器将导航到新页面,否则将取消导航。

What does "leave the page" mean? “离开页面”是什么意思? Back button? 返回键? X out? X出? Is it an event listener issue? 是事件侦听器问题吗?

According to the specification, to show the confirmation dialog an event handler should call preventDefault() on the event. 根据规范,要显示确认对话框,事件处理程序应在事件上调用preventDefault()。

I have that in place so that doesn't seem to be the issue... 我已经准备好了,所以这似乎不是问题所在...

Why doesn't the confirm modal launch? 为什么不确认模式启动?

Here is a full JS Fiddle demo: https://jsfiddle.net/g6wjrcae/ 这是完整的JS Fiddle演示: https : //jsfiddle.net/g6wjrcae/

In the conditional you add: 在条件中添加:

event.returnValue = "";
            return "";

So: 所以:

window.addEventListener('beforeunload', (event) => {
        event.preventDefault();
        if (window.formInputChanged) {
            event.returnValue = "";
            return "";
        }
    });

Try this in handler for "beforeunload" 在处理程序中尝试"beforeunload"

window.addEventListener("beforeunload", function (e) {
    if(formInputChanged) {
        var confirmationMessage = "You have unsaved changes";

        (e || window.event).returnValue = confirmationMessage; //Gecko + IE
        return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
    }
}); 

I believe confirm("You have unsaved changes"); 我相信confirm("You have unsaved changes"); is where your issue is. 是您的问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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