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


当前回答

我的jQuery解决方案。它可能不是100%可靠,但它对我有用。这个想法在代码注释中描述。

/***防止字段自动填充字段。*当关注具有自动完成功能的文本字段(值为:“off”、“none”、“false”)时,我们将该值替换为新的唯一值(此处为-“off forced-[TIMESTAMP]”),*浏览器在保存的值中找不到这种类型的自动完成,并且不提供选项。*然后,为了防止输入的文本保存在浏览器中,以便进行新的唯一自动完成,当字段失去焦点或用户按下Enter键时,我们将其替换为先前设置的文本。*@type{{init:*}}*/var PreventFieldsAutofill=(函数(){函数init(){events.onPageStart();}var事件={onPageStart:函数(){$(document).on('fous','输入[autocomplete=“off”],输入[autocomplete=“none”],input[autocomplete=“false”]',函数(){methods.replaceAttrs($(this));});$(document).on('blur','input[data prev autocomplete]',函数(){methods.returnAttrs($(this));});$(document).on('keydown','input[data prev autocomplete]',函数(事件){if(event.keyCode==13 | | event.which==13){methods.returnAttrs($(this));}});$(document).on('submit','form',函数(){$(this).find('input[data prev autocomplete]').each(函数(){methods.returnAttrs($(this));});});}};var方法={/***将autocomplete和name属性的值替换为unique,并将原始值保存为新的数据属性*@param$输入*/replaceAttrs:函数($input){var randomString='off-forced-'+Date.now();$input.attr('data-prev-autocomplete',$input.aattr('自动完成'));$input.attr('自动完成',随机字符串);if($input.attr('name')){$input.attr('data-prev-name',$input.atr('name'));$input.attr('name',randomString);}},/***恢复原始自动完成和名称值,以防止在浏览器中保存文本以获取唯一值*@param$输入*/returnAttrs:函数($input){$input.attr('自动完成',$input.atr('数据-版本-自动完成'));$input.removeAttr('data-prev-autocomplete');if($input.attr('data-prev-name')){$input.attr('name',$input.atr('data-prev-name'));$input.removeAttr('data-prev-name');}}};返回{init:初始化}})();PreventFieldsAutofill.init();.输入{显示:块;宽度:90%;填充:6px 12px;字体大小:14px;线高:1.42857143;颜色:#555555;背景色:#fff;背景图像:无;边框:1px实心#ccc;边界半径:4px;}<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js“></script><form action=“#”><p><label for=“input-1”>Firts name without autocomplete</label><br/><input id=“input-1”class=“input”type=“text”name=“first name”autocomplete=“off”placeholder=“Firts name without autocomplete”/></p><p><label for=“input-2”>Firts name with autocomplete</label><br/><input id=“input-2”class=“input”type=“text”name=“first name”autocomplete=“given name”placeholder=“Firts name with autocomplete”/></p><p><button type=“submit”>提交表单</button></p></form>

其他回答

我真不敢相信,这件事被报道了这么久,仍然是一个问题。以前的解决方案对我来说不起作用,因为Safari似乎知道元素何时不显示或不在屏幕上,但下面的解决方案确实对我起作用:

<div style="height:0px; overflow:hidden; ">
  Username <input type="text" name="fake_safari_username" >
  Password <input type="password" name="fake_safari_password">
</div>

只需将值为“off”的属性autocomplete设置为输入类型。

<input type="password" autocomplete="off" name="password" id="password" />

最简单的答案是

<input autocomplete=“on | off”>

但请记住浏览器支持。当前,支持自动完成属性

铬17.0及最新版本IE 5.0及最新版本Firefox 4.0及最新版本Safari 5.2及最新版本Opera 9.6及最新版本

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

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

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

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

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

这是有效的。

干杯

要防止浏览器自动填充用户保存的站点登录凭据,请在表单顶部放置一个文本和密码输入字段,该字段具有非空值,并设置样式“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>