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


当前回答

这对我有用。

<input name="pass" type="password" autocomplete="new-password" />

我们也可以在文本、选择等其他控件中使用此策略

其他回答

<form name="form1" id="form1" method="post"
      autocomplete="off" action="http://www.example.com/form.cgi">

这将在Internet Explorer和Mozilla Firefox中运行。缺点是它不是XHTML标准。

这个问题只需要自动完成属性您可以访问此页面了解更多信息

<input type="text" name="foo" 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>元素。

为了解决这个问题,我使用了一些CSS技巧和以下工作。

input {
    text-security:disc;
    -webkit-text-security:disc;
    -mox-text-security:disc;
}

请阅读本文了解更多详情。

要防止浏览器自动填充用户保存的站点登录凭据,请在表单顶部放置一个文本和密码输入字段,该字段具有非空值,并设置样式“position:aabsolute;top:-999px;left:-999px”以隐藏字段。

<form>
  <input type="text" name="username_X" value="-" tabindex="-1" aria-hidden="true" style="position: absolute; top: -999px; left:-999px" />
  <input type="password" name="password_X" value="-" tabindex="-1" aria-hidden="true" style="position: absolute; top: -999px; left:-999px" />
  <!-- Place the form elements below here. -->
</form>

在密码字段之前有一个文本字段很重要。否则,在某些情况下可能无法阻止自动填充。

重要的是文本和密码字段的值不能为空,以防止在某些情况下覆盖默认值。

这两个字段必须在表单中的“真实”密码类型字段之前。

对于兼容html 5.3的较新浏览器,自动完成属性值“新密码”应该有效。

<form>
  <input type="text" name="username" value="" />
  <input type="password" name="password" value="" autocomplete="new-password" />
</form>

这两种方法的组合可用于支持较旧和较新的浏览器。

<form>
  <div style="display:none">
    <input type="text" readonly tabindex="-1" />
    <input type="password" readonly tabindex="-1" />
  </div>
  <!-- Place the form elements below here. -->
  <input type="text" name="username" value="" />
  <input type="password" name="password" value="" autocomplete="new-password" />
</form>