简体   繁体   English

仅在“成功”登录页面上单击“后退”按钮时,而不是在“错误”页面上单击后,才重置表单

[英]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.

相关问题 单击“后退”按钮并登陆到具有超时重定向的页面上 - Clicking the back button and landing on a page with a timeout redirect to the same page 发送表单信息时出现登录页面错误 - landing page error in when sending form info 按下浏览器后退按钮时需要将用户导航到登录页面 - Need to navigate users to landing page when browser back button is pressed 从登录页面重定向到完整注册页面时自动填写表单内容 - Autofill form contents when redirecting from landing page to full registration page 仅在关闭浏览器时提示,而不在单击“保存”按钮时或使用javascript离开页面时提示? - Prompt only when closing browser not when clicking save button or when navigating away from page using javascript? 仅当用户来自该页面时,如何在提交表单后将 CodeIgniter 中的 URL 重定向回上一页? - How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page? 单击按钮时如何从页面(Bootstrap 4)中隐藏或删除搜索表单? - How to hide or remove the search form from the page (Bootstrap 4) on clicking a button? 如何仅在单击扩展程序中的按钮时将 CSS 添加到内容页面? - How to add the CSS to content page only on clicking of a button from an extension? 从后退按钮访问页面时强制页面刷新 - Force a page refresh when page visited from back button JavaScript-“向下滚动”按钮-从着陆页滚动到标题顶部 - JavaScript - “Scroll Down” Button - Scroll From Landing Page to Top of the Header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM