[英]Reset form only when clicking back button from SUCCESS landing page, NOT from ERROR page
I have a form. 我有一个表格。
Successful submission will load the success .html page. 成功提交将加载成功的 .html页面。
If there is an error (empty field or invalid email address) it will load the error .html page. 如果有错误(空白字段或无效的电子邮件地址),它将加载错误的 .html页面。
I'd like to reset the form (empty all fields) only if a visitor presses the browser's back button from the success .html. 仅当访客从成功的 .html中按下浏览器的后退按钮时,我才想重置表单(所有字段均为空白)。 If a visitor presses the BACK button on the error page the form should not be reset, ie the browser should behave as it normally does (default).
如果访问者在错误页面上按BACK按钮,则不应重置该表单,即,浏览器的行为应与通常情况相同(默认)。
This code resets the form when somebody presses the back button to get back to the form: 当有人按下“后退”按钮返回到表单时,此代码将重置表单:
$(window).bind("pageshow", function() {
document.form_name.reset();
});
So how can I make the form page aware of where the visitor came back from (when people press the browser's back button)? 那么,如何使表单页面知道访问者来自何处(当人们按下浏览器的后退按钮时)?
I thought I might define a variable (if true then reset, false on error page, true on success page) but for some reason this does not work. 我以为我可以定义一个变量(如果为true,然后重设,在错误页面上为false,在成功页面上为true),但由于某种原因,这不起作用。 What is the best practice here?
最佳做法是什么?
Thank you for your help. 谢谢您的帮助。
Ok, I think I worked it out: 好的,我想我已经解决了:
on the success page I add one JS line: 在成功页面上,我添加了一条JS行:
top.name='success';
And on the page with the form I add this: 然后在带有表单的页面上添加以下代码:
$(window).bind("pageshow", function() {
if (top.name === "success") {
document.form_name.reset();
top.name="";}
});
It works for me if JavaScript is enabled. 如果启用了JavaScript,它对我有用。 Should work in most browsers.
应该适用于大多数浏览器。 Maybe not the most elegant solution + depends on JS being enabled but I couldn't think of any other that's working.
也许不是最优雅的解决方案+取决于是否启用了JS,但我想不到其他任何有效的方法。 I read that the window.name method is not secure (other pages can read the window.name) but then the word 'success' is not really highly classified information.
我读到window.name方法并不安全(其他页面可以读取window.name),但是“成功”一词并不是真正高度机密的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.