简体   繁体   English

重新加载或关闭窗口时提交确认提示,但提交表单时不提示

[英]Confirmation alert when reloading or closing the window but not when submitting a form

I have created a form and using the window "onbeforeunload" feature, I've decided to add a confirmation box so that every time the user tries closing the window or reloading the page he gets an alert message. 我创建了一个表单,并使用窗口的“ onbeforeunload”功能,我决定添加一个确认框,以便每次用户尝试关闭窗口或重新加载页面时,都会收到一条警告消息。

The problem is that the message is something along the lines of "changes may not be saved" and that same confirmation error pops up when i'm submitting the form! 问题是消息是“更改可能无法保存”,并且在我提交表单时弹出相同的确认错误!

Is there a way i can get these confirmation pop ups everytime other than when submitting the form? 除了提交表格时,是否有其他方法可以使我每次弹出确认窗口?

I've seen some stack exchange questions wherein the OP has asked for an alert when reloading but not closing tab or vice versa. 我已经看到了一些堆栈交换问题,其中OP在重新加载时要求发出警报,但没有关闭选项卡,反之亦然。 Even in those questions the answer seems to be along the lines of "..It isn't possible..". 即使在这些问题中,答案似乎也符合“ ..不可能”。 Have a look at this question, for instance. 例如,看看这个问题。

So, I have a feeling it might not be possible here too, but is there any other way? 因此,我觉得在这里也可能无法实现,但是还有其他方法吗? For example, i could make the submit button as a normal button and then add some features manually? 例如,我可以将“提交”按钮设置为普通按钮,然后手动添加一些功能?

I have knowledge of JavaScript/HTML/CSS and nothing more. 我了解JavaScript / HTML / CSS,仅此而已。 Perhaps I could learn another language which would make it plausible? 也许我可以学习另一种使它看起来合理的语言? Or maybe there is an intrinsic feature I still don't know about? 还是有一个我仍然不知道的固有功能?

It would be preferable if I could somehow achieve it without using another language but I'm fine if that is the only option. 如果我能以某种方式不使用另一种语言来实现它,那将是更好的选择,但是我认为这是唯一的选择。

You can add a onclick="dontShowAnythingAndSubmit()" to your submit button. 您可以将onclick="dontShowAnythingAndSubmit()"到提交按钮。 Then move the submit button outside the form. 然后将提交按钮移到表单外部。

Define your function to override the onbeforeunload. 定义函数以覆盖onbeforeunload。

function dontShowAnythingAndSubmit() {
    window.onbeforeunload = null;
    document.getElementById("yourForm").submit(); 
}

Or did I not completely understand your question? 还是我不完全理解您的问题?

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

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