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


当前回答

我已经在这场永无止境的战斗中战斗了很久。。。所有的技巧和黑客最终都会停止工作,就像浏览器开发人员正在阅读这个问题一样。

我不想随机化域名,不想修改服务器端代码,不想使用JS重磅技巧,也不想将“黑客攻击”降至最低。下面是我想到的:

TL;DR使用完全没有名称或id的输入!并跟踪隐藏字段中的更改

<!-- input without the "name" or "id" -->
<input type="text" oninput="this.nextElementSibling.value=this.value">
<input type="hidden" name="email" id="email">

显然,可以在所有主要浏览器中使用。

P.S.已知的小问题:

您不能再通过id或名称引用此字段。但您可以使用CSS类。或者使用$(#email').prev();在jQuery中。或者想出另一个解决方案(有很多)。当以编程方式更改文本框值时,oninput和onchange事件不会触发。因此,相应地修改代码以反映隐藏字段中的更改。

其他回答

我的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>

正如其他人所说,答案是autocomplete=“off”。

然而,我认为有必要说明为什么在某些情况下使用此选项是一个好主意,因为对此问题和重复问题的一些答案表明,最好不要关闭此选项。

停止存储信用卡号的浏览器不应该留给用户。太多的用户甚至不会意识到这是一个问题。

在信用卡安全码字段上关闭它尤为重要。如本页所述:

“永远不要存储安全码……它的价值取决于这样一种假设,即提供安全码的唯一方法是从实体信用卡中读取安全码,以证明提供该安全码的人实际上持有该卡。”

问题是,如果是公共计算机(网吧、图书馆等),其他用户很容易窃取您的卡信息,甚至在您自己的计算机上,恶意网站也可能窃取自动完成的数据。

只需设置autocomplete=“off”。这样做有一个很好的理由:您希望提供自己的自动完成功能!

除了

autocomplete="off"

使用

readonly onfocus="this.removeAttribute('readonly');"

对于不希望他们记住表单数据(用户名、密码等)的输入,如下所示:

<input type="text" name="UserName" autocomplete="off" readonly 
    onfocus="this.removeAttribute('readonly');" >

<input type="password" name="Password" autocomplete="off" readonly 
    onfocus="this.removeAttribute('readonly');" >

这里提到的黑客在Chrome中没有一个对我有用。这里讨论了这个问题:https://code.google.com/p/chromium/issues/detail?id=468153#c41

将其添加到<form>中(至少目前如此):

<div style="display: none;">
    <input type="text" id="PreventChromeAutocomplete" name="PreventChromeAutocomplete" autocomplete="address-level4" />
</div>