Html 制作页面以告诉浏览器不要缓存/保留输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2699284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make page to tell browser not to cache/preserve input values
提问by queen3
Most browser cache form input values. So when user refreshes page, the inputs have same values.
大多数浏览器缓存表单输入值。因此,当用户刷新页面时,输入具有相同的值。
Here's my problem. When user clicks Save, server validates POSTed data (e.g. checked products), and if not valid, sends it back to browser. However, as stated above, even if server clears selection for some values, they may still be selected because of browser cache! My data has invisible (until parent item selected) checkboxes, so user may be even not aware that some previous value is still selected, until clicks Save again and gets error message - even though user thinks it's not. Which is irritating.
这是我的问题。当用户点击保存时,服务器验证 POSTed 数据(例如检查的产品),如果无效,则将其发送回浏览器。但是,如上所述,即使服务器清除了某些值的选择,但由于浏览器缓存,它们仍然可能被选中!我的数据有不可见的(直到选择父项)复选框,因此用户甚至可能不知道某些先前的值仍然被选中,直到再次单击“保存”并收到错误消息 - 即使用户认为它不是。这是令人恼火的。
This can be resolved by doing Ctrl-F5, but it's not even a solution. Is there an automatic/programmatic way to tell browser not to cache form input data on some form/page?
这可以通过执行 Ctrl-F5 来解决,但这甚至不是解决方案。是否有一种自动/编程方式告诉浏览器不要在某些表单/页面上缓存表单输入数据?
回答by DisgruntledGoat
Are you explicitly setting the values as blank? For example:
您是否明确将值设置为空白?例如:
<input type="text" name="textfield" value="">
That should stop browsers putting data in where it shouldn't. Alternatively, you can add the autocomplete
attribute to the form tag:
这应该会阻止浏览器将数据放在不该放的地方。或者,您可以将autocomplete
属性添加到表单标签:
<form autocomplete="off" ...></form>
回答by shareef
it did not work with value="" if the browser already save the value so you should add
如果浏览器已经保存了该值,则它不适用于 value="",因此您应该添加
For a input tag there's the attribute autocomplete you can set:
对于输入标签,您可以设置属性自动完成:
<input type="text" autocomplete="off" />
You can use autocomplete for a form too.
您也可以对表单使用自动完成功能。
回答by pmko
another approach would be to reset the form using JavaScript right after the form in the HTML.
另一种方法是在 HTML 中的表单之后立即使用 JavaScript 重置表单。
<form id="myForm">
<input type="text" value="" name="myTextInput" />
</form>
<script type="text/javascript">
document.getElementById("myForm").reset();
</script>
回答by Anand Dwivedi
Basically, There are two ways to clear the cache:
基本上,有两种方法可以清除缓存:
<form autocomplete="off"></form>
or
或者
$('#Textfiledid').attr('autocomplete','off');
`
`
回答by erikrunia
late add, but this worked for me in newer browsers:
晚添加,但这在较新的浏览器中对我有用:
autocomplete="new-password"
自动完成=“新密码”