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


当前回答

由于自动完成的行为在不同的浏览器和版本中几乎是不可预测的,所以我的方法是不同的。指定要禁用只读属性自动填充的所有输入元素,并仅在聚焦时禁用它。

  document.addEventListener('click', (e) => {
    readOnlys.forEach(readOnly => {
      if (e.target == readOnly) {
        readOnly.removeAttribute('readonly', '');
        readOnly.style.setProperty('pointer-events', 'none');
      } else {
        readOnly.setAttribute('readonly', '');
        readOnly.style.setProperty('pointer-events', 'auto');
      }
    });
  });
  document.addEventListener('keyup', (e) => {
    if (e.key == 'Tab') {
      readOnlys.forEach(readOnly => {
        if (e.target == readOnly) {
          readOnly.removeAttribute('readonly', '');
          readOnly.style.setProperty('pointer-events', 'none');
        } else {
          readOnly.setAttribute('readonly', '');
          readOnly.style.setProperty('pointer-events', 'auto');
        }
      });
    }
  });

如果您想确保用户在禁用JS的情况下仍然可以访问字段,那么可以在页面加载时通过JS设置所有只读。您仍然可以使用autocomplete属性作为回退。

其他回答

对于React,您可以尝试将此代码放在表单下面或密码输入上面,或者放在电子邮件和密码输入之间

export const HackRemoveBrowsersAutofill = () => (
  <>
    <input type="email" autoComplete="new-password" style={ { display: 'none' } } />
    <input type="password" autoComplete="new-password" style={ { display: 'none' } } />
  </>
)

示例之一:

<input type="email"/>
<HackRemoveBrowsersAutofill/>
<input type="password"/>

如果您的问题是自动填写密码字段,那么您可能会发现这很有用。。。

我们在网站的几个领域遇到了这个问题,在这些领域中,业务部门希望重新查询用户的用户名和密码,特别是出于合同原因,不希望自动填充密码。我们发现,最简单的方法是输入一个假密码字段,以便浏览器查找和填充,而真实密码字段保持不变。

<!-- This is a fake password input to defeat the browser's autofill behavior -->
<input type="password" id="txtPassword" style="display:none;" />
<!-- This is the real password input -->
<input type="password" id="txtThisIsTheRealPassword" />

请注意,在Firefox和IE中,将任何类型的密码输入放在实际输入之前就足够了,但Chrome识破了这一点,并迫使我实际命名假密码输入(通过给它一个明显的密码id),以使其“咬”。我使用了一个类来实现样式,而不是使用嵌入样式,所以如果由于某种原因,上面的方法不起作用,请尝试这样做。

没有虚假输入,没有javascript!

无法在浏览器之间一致禁用自动填充。我尝试了所有不同的建议,但没有一个在所有浏览器中都有效。唯一的方法是根本不使用密码输入。下面是我想到的:

<style type="text/css">
    @font-face {
        font-family: 'PasswordDots';
        src: url('text-security-disc.woff') format('woff');
        font-weight: normal;
        font-style: normal;
    }

    input.password {
        font-family: 'PasswordDots' !important;
        font-size: 8px !important;
    }
</style>

<input class="password" type="text" spellcheck="false" />

下载:text-security-disc.woff

以下是我的最终结果:

负面的副作用是,可以从输入中复制纯文本,但使用一些JS应该可以防止这种情况。

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

我终于找到了一个纯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>

关于InternetExplorer11,有一个安全功能可以用来阻止自动完成。

它的工作原理如下:

用户输入后在JavaScript中修改的任何表单输入值都被标记为不符合自动完成的条件。

此功能通常用于保护用户免受恶意网站的攻击,这些网站在您输入密码或类似内容后想要更改密码。

但是,您可以在密码字符串的开头插入一个特殊字符来阻止自动完成。这个特殊字符可以被检测到,并在以后的管道中被删除。