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


当前回答

我使用此TextMode=“password”autocomplete=“new password”,并在aspx txtPassword.Attributes.Add(“value”,“”)中加载页面;

其他回答

有时,即使autocomplete=off也无法阻止将凭据填写到错误的字段中,而不是用户或昵称字段。

除了apinstein关于浏览器行为的帖子之外,还有一个变通方法。

修复浏览器自动填充为只读,并在焦点上设置为可写(单击并选项卡)

 <input type="password" readonly
     onfocus="this.removeAttribute('readonly');"/>

更新:

Mobile Safari在字段中设置光标,但不显示虚拟键盘。新的修复程序与以前一样,但它处理虚拟键盘:

<input id="email" readonly type="email" onfocus="if (this.hasAttribute('readonly')) {
    this.removeAttribute('readonly');
    // fix for mobile safari to show virtual keyboard
    this.blur();    this.focus();  }" />

现场演示https://jsfiddle.net/danielsuess/n0scguv6/

//更新结束

因为浏览器自动将凭据填充到错误的文本字段!?

我注意到在Chrome和Safari上,当相同的表单中有密码字段时,会出现这种奇怪的行为。我猜浏览器会寻找一个密码字段来插入您保存的凭据。然后它会自动填充(只是由于观察而猜测)最近的类似文本的输入字段,该字段出现在DOM中的密码字段之前。因为浏览器是最后一个实例,您无法控制它。

上面的这个只读修复对我有用。

我一直在尝试无尽的解决方案,然后我发现:

不用autocomplete=“off”,只需使用autocomplete==“false”

就这么简单,它在谷歌Chrome中也很有魅力!

许多现代浏览器不再支持登录字段的autocomplete=“off”。autocomplete=“新密码”是wokring,更多信息MDN文档

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

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

其想法是创建一个与原始字段同名的不可见字段。这将使浏览器自动填充隐藏字段。

我使用以下jQuery片段:

// Prevent input autocomplete
$.fn.preventAutocomplete = function() {
    this.each(function () {
        var $el = $(this);
        $el
            .clone(false, false) // Make a copy (except events)
            .insertBefore($el)   // Place it before original field
            .prop('id', '')      // Prevent ID duplicates
            .hide()              // Make it invisible for user
        ;
    });
};

然后只需$(“#登录表单输入”).prpreventAutocomplete();