禁用浏览器当点击“返回”时输入预填充/自动填充功能。按键 [英] Disable browsers Form inputs prefill/autofill feature when hitting "back" button

查看:201
本文介绍了禁用浏览器当点击“返回”时输入预填充/自动填充功能。按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止浏览器在点击返回按钮时预先填写表单输入。事实上,我想要填充初始值(通过jsp添加),而不是浏览器的(缓存)值。

解决方案1:我发现这可以是通过禁用当前页面的浏览器缓存来完成。这似乎是一个相当极端的解决方案,因为我只想要禁用此预填功能的表单(因此禁止缓存仅用于表单,而不是整个页面)。



解决方案2:然后,下一个明显的解决方案是使用javascript:即将初始值存储在data- *属性中,然后在页面加载时将输入值替换为初始值(如果它们不同)。 / p>

这两个解决方案看起来相当不完美(这些都是解决方法)我转向你们,希望听到更好的解决方案。



资源:




解决方案

我想到的第一件事是使用< input type =reset> 按钮。由于用户很少真正想要重置表单,因此这些在当今时常不常见,但在这里它可能就是您需要的。



您也可以在javascript中执行此操作在页面加载时使用 form.reset(); 而不是为用户提供一个按钮。



https://developer.mozilla.org/zh-CN/docs/Web/ API / HTMLFormElement.reset



这与您的解决方案2类似,因此仍然是浏览器行为的解决方法,但它是(旧)部分的标准形式,我认为它会用很少的附加代码(不需要data- *属性)来实现,所以想把它扔到那里。


I want to "prevent browsers from prefilling form inputs when hitting the "back" button". Indeed, I want the initial values to be filled in (added via jsp), not the browser's (cached) values.

Solution 1: I found out that this can be done by disabling the browser caching for the current page. This seems a rather extreme solution considering that I "only" want to disable this prefill feature for a "form" (hence disable caching for the form only, not the whole page).

Solution 2: Then, the obvious next solution is to use javascript: that is, store the initial value in a data-* attribute, then, on page load, replace the input value by the initial value if they differ.

Both solutions seem rather unperfect (these are rather work arounds) I turn to you guys hoping to hear of a better one.

Resources:

解决方案

The first thing that comes to my mind is to use a <input type="reset"> button. These aren't seen often nowadays because the user rarely actually wants to reset the form, but here it might just be what you need.

You could also do it in javascript on page load with form.reset(); instead of providing a button for the user.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement.reset

This is similar to your solution 2 and thus still a workaround of the browser behavior, but it is a(n old) part of standard forms and I think it'll do the trick with very little additional code (no need for data-* attributes), so wanted to throw it out there.

这篇关于禁用浏览器当点击“返回”时输入预填充/自动填充功能。按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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