当使用xhtml1-transitional。使用以下HTML收集一个信用卡号
<input type="text" id="cardNumber" name="cardNumber" autocomplete='off'/>
将在W3C验证器上标记一个警告:
没有“自动完成”属性。
是否有一种符合标准的方法来禁用浏览器自动完成表单中的敏感字段?
当使用xhtml1-transitional。使用以下HTML收集一个信用卡号
<input type="text" id="cardNumber" name="cardNumber" autocomplete='off'/>
将在W3C验证器上标记一个警告:
没有“自动完成”属性。
是否有一种符合标准的方法来禁用浏览器自动完成表单中的敏感字段?
当前回答
下面是一篇来自MDC的好文章,它解释了形成自动补全的问题(和解决方案)。 微软在这里也发布了类似的东西。
老实说,如果这对你的用户很重要,用这种方式“打破”标准似乎是合适的。例如,亚马逊使用“自动完成”属性相当多,它似乎工作得很好。
如果您想完全删除警告,可以使用JavaScript使用someForm将该属性应用到支持它的浏览器(IE和Firefox是重要的浏览器)。setAttribute("autocomplete", "off");someFormElm。setAttribute("autocomplete", "off");
最后,如果你的网站使用的是HTTPS, IE会自动关闭自动完成功能(据我所知,其他一些浏览器也是这样做的)。
更新
由于这个答案仍然得到了相当多的赞,我只是想指出,在HTML5中,你可以在你的表单元素上使用“autocomplete”属性。请参阅W3C上的文档。
其他回答
Autocomplete ="off"这应该解决所有现代浏览器的问题。
<form name="form1" id="form1" method="post" autocomplete="off"
action="http://www.example.com/form.cgi">
[...]
</form>
在当前版本的Gecko浏览器中,自动完成属性工作得很好。对于早期版本,即Netscape 6.2,它只适用于带有“地址”和“名称”的表单。
更新
在某些情况下,即使autocomplete属性被设置为关闭,浏览器也会继续建议自动补全值。这种出乎意料的行为可能会让开发人员感到困惑。真正强制no-autocompletion的技巧是为属性分配一个随机字符串,例如:
autocomplete="nope"
由于这个随机值不是有效值,浏览器将放弃。
Documetation
不理想,但你可以改变id和文本框的名称,每次你渲染它-你必须跟踪它的服务器端,这样你就可以得到数据。
不确定这是否会起作用,这只是一个想法。
我在2020年就做到了!
我基本上创建了一个css类,对我的输入应用-webkit-text-security。
这里有一个最近讨论的链接: https://stackoverflow.com/a/64471795/8754782
Note that there's some confusion about location of the autocomplete attribute. It can be applied either to the whole FORM tag or to individual INPUT tags, and this wasn't really standardized before HTML5 (that explicitly allows both locations). Older docs most notably this Mozilla article only mentions FORM tag. At the same time some security scanners will only look for autocomplete in INPUT tag and complain if it's missing (even if it is in the parent FORM). A more detailed analysis of this mess is posted here: Confusion over AUTOCOMPLETE=OFF attributes in HTML forms.
用JavaScript设置怎么样?
var e = document.getElementById('cardNumber');
e.autocomplete = 'off'; // Maybe should be false
这并不完美,但您的HTML将是有效的。