在离开网页之前提示用户填写表单 - 通过 Javascript
我的网页上有一个网络表单。在用户离开页面之前,我想要一条 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是其他人要求您做的事情,或者您认为这是一个好主意,但让我说,如果您有选择,请不要这样做。 如果用户想要离开页面,请让他们离开页面,而不告诉他们他们已经知道的事情。此外,一些现代浏览器已经内置了此功能,因此它只是给用户增加了另一层烦人的麻烦。
话虽如此,请查看此页面,它会告诉您需要了解的所有信息。
......
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.
...
21 件事(感谢 @horray 我正在帮忙)。您最好检测表单是否已以编程方式填写,然后如果用户单击继续而没有提供关键数据,请向他们询问。如果他们决定不提供,那就让他们走吧。
例如:
您可以将其放入链接的点击处理程序、提交按钮或您提到的 beforeunload 事件中。
21 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:
You could put that in click handlers for links, submit buttons or in the beforeunload event as you mentioned.