如何在主要浏览器中禁用特定输入(或表单字段)的自动完成?


当前回答

在这次谈话中,没有一个解决方案对我有效。

我终于找到了一个纯HTML解决方案,它不需要任何JavaScript,可以在现代浏览器中工作(除了Internet Explorer;至少要有一个捕获,对吗?),并且不需要对整个表单禁用自动完成。

只需关闭表单上的自动完成功能,然后为您希望在表单中工作的任何输入打开它。例如:

<form autocomplete="off">
    <!-- These inputs will not allow autocomplete and Chrome
         won't highlight them yellow! -->
    <input name="username"  />
    <input name="password" type="password" />
    <!-- This field will allow autocomplete to work even
         though we've disabled it on the form -->
    <input name="another_field" autocomplete="on" />
</form>

其他回答

除了设置autocomplete=off之外,您还可以通过生成页面的代码随机化表单字段名称,或者在名称末尾添加一些特定于会话的字符串。

提交表单后,您可以在服务器端处理表单之前剥离该部分。这将阻止web浏览器查找字段的上下文,也可能有助于防止XSRF攻击,因为攻击者无法猜测表单提交的字段名称。

始终有效的解决方案

我用随机字符解决了与谷歌Chrome的无休止斗争。当您总是使用随机字符串呈现自动完成时,它将永远不会记住任何内容。

<input name="name" type="text" autocomplete="rutjfkde">

希望这对其他人有所帮助。

2022年更新:

Chrome做出了这一改进:autocomplete=“新密码”,这将解决这个问题,但我不确定,如果一段时间后Chrome再次将其更改为不同的功能。

目前我找到的解决方案都没有带来真正有效的回应。

具有自动完成属性的解决方案不起作用。

所以,这是我为自己写的:

<input type="text" name="UserName" onkeyup="if (this.value.length > 0) this.setAttribute('type', 'password'); else this.setAttribute('type', 'text');" >

您应该对页面上希望作为密码类型的每个输入字段执行此操作。

这是有效的。

干杯

Chrome计划支持这一点。

目前,最好的建议是使用很少自动完成的输入类型。

Chrome讨论

<input type='search' name="whatever" />

要与Firefox兼容,请使用normalautocomplete='off'

<input type='search' name="whatever" autocomplete='off' />

当我自己尝试时,事情已经改变了,旧的答案不再有效。

我确信它会奏效。我在Chrome、Edge和Firefox中测试了这一功能,它确实做到了这一点。你也可以尝试一下,告诉我们你的经历。

set the autocomplete attribute of the password input element to "new-password"

<form autocomplete="off">
....other element
<input type="password" autocomplete="new-password"/>
</form>

这是根据MDN

如果您正在定义一个用户管理页面,用户可以在该页面中为另一个人指定新密码,因此您希望防止自动填充密码字段,则可以使用autocomplete=“新密码”

这是一个提示,哪些浏览器不需要遵守。然而,由于这个原因,现代浏览器已经停止使用autocomplete=“new password”自动填充<input>元素。