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


当前回答

<input type="text" name="attendees" id="autocomplete-custom-append">

<script>
  /* AUTOCOMPLETE */

  function init_autocomplete() {

    if (typeof ($.fn.autocomplete) === 'undefined') {
      return;
    }
    // console.log('init_autocomplete');

    var attendees = {
      1: "Shedrack Godson",
      2: "Hellen Thobias Mgeni",
      3: "Gerald Sanga",
      4: "Tabitha Godson",
      5: "Neema Oscar Mracha"
    };

    var countriesArray = $.map(countries, function (value, key) {
      return {
        value: value,
        data: key
      };
    });

    // initialize autocomplete with custom appendTo
    $('#autocomplete-custom-append').autocomplete({
      lookup: countriesArray
    });

  };
</script>

其他回答

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

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

<!-- 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),以使其“咬”。我使用了一个类来实现样式,而不是使用嵌入样式,所以如果由于某种原因,上面的方法不起作用,请尝试这样做。

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

我使用以下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();

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

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

我不想随机化域名,不想修改服务器端代码,不想使用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事件不会触发。因此,相应地修改代码以反映隐藏字段中的更改。

最佳解决方案:

防止自动完成用户名(或电子邮件)和密码:

<input type="email" name="email"><!-- Can be type="text" -->
<input type="password" name="password" autocomplete="new-password">

阻止自动完成字段:

<input type="text" name="field" autocomplete="nope">

说明:autocomplete在<input>中继续工作,autocomplete=“off”不工作,但您可以将off更改为随机字符串,如nope。

工作地点:

铬:49、50、51、52、53、54、55、56、57、58、59、60、61、62、63和64Firefox:44、45、46、47、48、49、50、51、52、53、54、55、56、57和58