在离开网页之前提示用户填写表单 - 通过 Javascript

发布于 2024-09-13 21:45:36 字数 284 浏览 6 评论 0原文

我的网页上有一个网络表单。在用户离开页面之前,我想要一条 JavaScript 弹出消息,询问用户是否已完成带有“是”或“否”按钮的表单。如果用户单击“是”,他们就会被带到他们想要访问的页面。但是,如果用户单击“否”,则用户仍保留在此页面上。

我对 Javascript 不太熟悉,所以任何指导将不胜感激。我怀疑我会使用如下内容:

<ELEMENT onbeforeunload = "handler" ... >

提前致谢。

斯普利特 P。

I have a web form on my web page. Before the user leaves the page, I want a javascript popup message asking the user if they have completed the form with a "Yes" or "No" button on it. If the user clicks "Yes", then they are brought to the page they intended to go to. However, if the user clicks "No", then the user remains on this page.

I am not very familiar with Javascript so any guidance would be much appreciated. I am suspecting that I would use something like below:

<ELEMENT onbeforeunload = "handler" ... >

Thanks in advance.

Split P.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-09-20 21:45:36

我不确定这是否是其他人要求您做的事情,或者您认为这是一个好主意,但让我说,如果您有选择,请不要这样做。 如果用户想要离开页面,请让他们离开页面,而不告诉他们他们已经知道的事情。此外,一些现代浏览器已经内置了此功能,因此它只是给用户增加了另一层烦人的麻烦。

话虽如此,请查看此页面,它会告诉您需要了解的所有信息。

onbeforeunload 的奇怪之处在于,如果您在事件处理程序中返回任何内容,它会弹出一个确认警报框,询问用户是否真的确定要离开页面

......

您可以通过在 return 语句中包含一个字符串来在这两行之间插入您自己的解释文本。例如:

<script type="text/javascript">
  window.onbeforeunload = function () {
    return "The text you want the alert box to say.";
  }
</script>

I'm not sure if this is something you're required to do by someone else, or something you think would be a good idea, but let me say that if you have the choice, please don't do this. If the user wants to leave the page, let them leave the page without telling them something they already know. Further, several modern browsers already have this functionality baked in, so it is just adding another layer of annoying for the user.

Having said that, take a look at this page, which will tell you everything you need to know.

What makes onbeforeunload quirky is that if you return ANYTHING at all in your event handler it will pop up a confirmation alert box asking the user if he or she is REALLY sure they want to leave the page

...

You can insert your own explanatory text BETWEEN those two lines by including a string on the return statement. For instance:

<script type="text/javascript">
  window.onbeforeunload = function () {
    return "The text you want the alert box to say.";
  }
</script>
疑心病 2024-09-20 21:45:36

2 1 件事(感谢 @horray 我正在帮忙)。

您最好检测表单是否已以编程方式填写,然后如果用户单击继续而没有提供关键数据,请向他们询问。如果他们决定不提供,那就让他们走吧。

例如:

var requiredField = document.getElementById("required");
if (requiredField.value == "")
{
    if (!confirm("Are you sure you want to go without saving your work?")){
       requiredField.focus();
    }
}

您可以将其放入链接的点击处理程序、提交按钮或您提到的 beforeunload 事件中。

2 1 thing (thanks @horray I'm helping).

You're better off detecting whether the form has been filled out programmatically, then if the user clicks to continue on without providing a critical piece of data, ask them for it. If they decide not to provide it, let them go.

For example:

var requiredField = document.getElementById("required");
if (requiredField.value == "")
{
    if (!confirm("Are you sure you want to go without saving your work?")){
       requiredField.focus();
    }
}

You could put that in click handlers for links, submit buttons or in the beforeunload event as you mentioned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文