禁用输入字段自动填充(纯 JS)

Posted

技术标签:

【中文标题】禁用输入字段自动填充(纯 JS)【英文标题】:Disable input field autofill (pure JS) 【发布时间】:2021-08-21 14:28:13 【问题描述】:

有谁知道如何在 2021 中使用纯 JS 禁用输入自动填充/自动完成功能?

我试过了:

<input type="password" autocomplete="off" />
<input type="password" autofill="off" />
<input type="password" autocomplete="new-password" />
<input type="password" autofill="none" />

还尝试使用 *** 解决方案中的 jquery.disable-autofillnpm disableautofill

如果有人可以分享更多解决方案,那就太好了

【问题讨论】:

【参考方案1】:

我们还可以在表单标签中使用自动完成功能,如下所示

<form action="/form/submit" method="get" autocomplete="off">

或者在 JS 中

    $(function() 
      setTimeout(function() 
        $("input").val("");
      , 500);
   );

【讨论】:

【参考方案2】:

在 JS 中尝试这个解决方案

clearAutoFIll();

function clearAutoFIll()
  var inp = document.querySelectorAll("[autocomplete=off] input");
  for(var x=0; x < inp.length; x++)
    inp[x].value = "";
  
<form autocomplete=off>
  <input type=text name="phone" />
</form>

【讨论】:

以上是关于禁用输入字段自动填充(纯 JS)的主要内容,如果未能解决你的问题,请参考以下文章