使用浏览器后退按钮返回时清除表单中的所有字段 [英] Clear all fields in a form upon going back with browser back button

查看:38
本文介绍了使用浏览器后退按钮返回时清除表单中的所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户使用浏览器后退按钮时,我需要一种方法来清除表单中的所有字段.现在,浏览器会记住所有最后的值,并在您返回时显示它们.

I need a way to clear all the fields within a form when a user uses the browser back button. Right now, the browser remembers all the last values and displays them when you go back.

更多说明为什么我需要这个我有一个禁用的输入字段,其值是使用算法自动生成的,以使其在特定数据组中是唯一的.一旦我提交了表单并将数据输入到数据库中,用户就不能再次使用相同的值来提交相同的表单.因此,我首先禁用了输入字段.但是如果用户使用浏览器的后退按钮,浏览器会记住最后一个值,并且相同的值会保留在输入字段中.因此用户可以再次提交具有相同值的表单.

More clarification on why I need this I've a disabled input field whose value is auto-generated using an algorithm to make it unique within a certain group of data. Once I've submitted the form and data is entered into the database, user should not be able to use the same value again to submit the same form. Hence I've disabled the input field in the first place. But if the user uses the browser back button, the browser remembers the last value and the same value is retained in the input field. Hence the user can submit the form with the same value again.

我不明白当您按下浏览器后退按钮时到底发生了什么.如果页面大小在浏览器缓存限制内,似乎整个页面都是从缓存中检索的,而无需联系服务器.当您按下浏览器后退按钮时,如何确保无论浏览器设置如何都从服务器加载页面?

What I don't understand is what exactly happens when you press the browser back button. It seem like the entire page is retrieved from cache without ever contacting the server if the page size is within the browser cache limit. How do I ensure that the page is loaded from the server regardless of browser setting when you press the browser back button?

推荐答案

现代浏览器实现了一种称为后向缓存 (BFCache) 的东西.当您点击后退/前进按钮时,实际页面不会重新加载(并且脚本永远不会重新运行).

Modern browsers implement something known as back-forward cache (BFCache). When you hit back/forward button the actual page is not reloaded (and the scripts are never re-run).

如果您必须在用户按下后退/前进键的情况下执行某些操作 - 监听 BFCache pageshowpagehide 事件:

If you have to do something in case of user hitting back/forward keys - listen for BFCache pageshow and pagehide events:

window.addEventListener("pageshow", () => {
  // update hidden input field
});

查看关于 GeckoWebKit 实现.

See more details for Gecko and WebKit implementations.

这篇关于使用浏览器后退按钮返回时清除表单中的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆