Make page to tell browser not to cache/preserve input values

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 (eg 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.

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?


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:

<form autocomplete="off" ...></form>

from stackoverflow reference

it did not work with value="" if the browser already save the value so you should add

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.


另一种方法是在HTML中的表单之后立即使用JavaScript重置表单。

<form id="myForm">
  <input type="text" value="" name="myTextInput" />
</form>
<script type="text/javascript">
  document.getElementById("myForm").reset();
</script>
链接地址: http://www.djcxy.com/p/56034.html

上一篇: 输入复选框为真或选中或是

下一篇: 让页面告诉浏览器不要缓存/保存输入值