简体   繁体   English

如何在 web 表单字段/输入标签上禁用浏览器自动完成功能?

[英]How do you disable browser autocomplete on web form field / input tags?

How do you disable autocomplete in the major browsers for a specific input (or form field)?如何在主要浏览器中禁用特定输入(或表单域)的自动完成功能?

Firefox 30 ignores autocomplete="off" for passwords, opting to prompt the user instead whether the password should be stored on the client. Firefox 30 忽略密码的autocomplete="off" ,而是选择提示用户是否应将密码存储在客户端上。 Note the following commentary from May 5, 2014:请注意 2014 年 5 月 5 日的以下评论

  • The password manager always prompts if it wants to save a password.密码管理器总是提示是否要保存密码。 Passwords are not saved without permission from the user.未经用户许可,不会保存密码。
  • We are the third browser to implement this change, after IE and Chrome.我们是继 IE 和 Chrome 之后第三个实现此更改的浏览器。

According to the Mozilla Developer Network documentation, the Boolean form element attribute autocomplete prevents form data from being cached in older browsers.根据Mozilla 开发者网络文档,布尔表单元素属性autocomplete可防止表单数据在旧版浏览器中缓存。

<input type="text" name="foo" autocomplete="off" />

In addition to setting autocomplete=off , you could also have your form field names be randomized by the code that generates the page, perhaps by adding some session-specific string to the end of the names.除了设置autocomplete=off ,您还可以通过生成页面的代码将表单字段名称随机化,可能通过在名称末尾添加一些特定于会话的字符串。

When the form is submitted, you can strip that part off before processing them on the server-side.提交表单后,您可以在服务器端处理它们之前剥离该部分。 This would prevent the web browser from finding context for your field and also might help prevent XSRF attacks because an attacker wouldn't be able to guess the field names for a form submission.这将阻止 Web 浏览器为您的字段查找上下文,还可能有助于防止 XSRF 攻击,因为攻击者无法猜测表单提交的字段名称。

Most of the major browsers and password managers (correctly, IMHO) now ignore autocomplete=off .大多数主要浏览器和密码管理器(正确,恕我直言)现在忽略autocomplete=off

Why?为什么? Many banks and other "high security" websites added autocomplete=off to their login pages "for security purposes" but this actually decreases security since it causes people to change the passwords on these high-security sites to be easy to remember (and thus crack) since autocomplete was broken.许多银行和其他“高安全性”网站“出于安全目的”在其登录页面中添加了autocomplete=off ,但这实际上降低了安全性,因为它导致人们在这些高安全性网站上更改密码很容易记住(从而破解) 因为自动完成功能被破坏了。

Long ago most password managers started ignoring autocomplete=off , and now the browsers are starting to do the same for username/password inputs only.很久以前,大多数密码管理器开始忽略autocomplete=off ,现在浏览器开始只对用户名/密码输入做同样的事情。

Unfortunately, bugs in the autocomplete implementations insert username and/or password info into inappropriate form fields, causing form validation errors, or worse yet, accidentally inserting usernames into fields that were intentionally left blank by the user.不幸的是,自动完成实现中的错误将用户名和/或密码信息插入到不适当的表单字段中,导致表单验证错误,或者更糟糕的是,意外地将用户名插入到用户故意留空的字段中。

What's a web developer to do?网络开发人员要做什么?

  • If you can keep all password fields on a page by themselves, that's a great start as it seems that the presence of a password field is the main trigger for user/pass autocomplete to kick in. Otherwise, read the tips below.如果您可以将所有密码字段单独保留在页面上,那将是一个很好的开始,因为似乎密码字段的存在是用户/密码自动完成的主要触发因素。否则,请阅读以下提示。
  • Safari notices that there are 2 password fields and disables autocomplete in this case, assuming it must be a change password form, not a login form. Safari注意到有 2 个密码字段并在这种情况下禁用自动完成,假设它必须是更改密码表单,而不是登录表单。 So just be sure to use 2 password fields (new and confirm new) for any forms where you allow因此,请确保在您允许的任何表单中使用 2 个密码字段(新密码和确认新密码)
  • Chrome 34, unfortunately, will try to autofill fields with user/pass whenever it sees a password field.不幸的是, Chrome 34 会在看到密码字段时尝试使用用户/密码自动填充字段。 This is quite a bad bug that hopefully, they will change the Safari behavior.这是一个非常糟糕的错误,希望他们会改变 Safari 的行为。 However, adding this to the top of your form seems to disable the password autofill:但是,将此添加到表单顶部似乎会禁用密码自动填充:

     <input type="text" style="display:none"> <input type="password" style="display:none">

I haven't yet investigated IE or Firefox thoroughly but will be happy to update the answer if others have info in the comments.我尚未彻底调查 IE 或 Firefox,但如果其他人在评论中有信息,我很乐意更新答案。

Sometimes even autocomplete=off would not prevent to fill in credentials into the wrong fields, but not a user or nickname field.有时,即使 autocomplete=off不会阻止将凭据填写到错误的字段中,但不会阻止用户或昵称字段。

This workaround is in addition to apinstein's post about browser behavior.此解决方法是对apinstein 关于浏览器行为的帖子的补充。

Fix browser autofill in read-only and set writable on focus (click and tab)修复浏览器自动填充只读并在焦点上设置可写(单击和选项卡)

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

Update:更新:

Mobile Safari sets cursor in the field, but it does not show the virtual keyboard. Mobile Safari 在字段中设置光标,但不显示虚拟键盘。 The new fix works like before, but it handles the virtual keyboard:新修复程序像以前一样工作,但它处理虚拟键盘:

<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();  }" />

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

// UpdateEnd // 更新结束

Because the browser auto fills credentials to wrong text field!?因为浏览器自动将凭据填充到错误的文本字段!?

I notice this strange behavior on Chrome and Safari, when there are password fields in the same form.当有相同形式的密码字段时,我注意到 Chrome 和 Safari 上的这种奇怪行为。 I guess the browser looks for a password field to insert your saved credentials.我猜浏览器会寻找一个密码字段来插入您保存的凭据。 Then it auto fills (just guessing due to observation) the nearest textlike-input field, that appears prior the password field in the DOM.然后它自动填充(只是根据观察猜测)最近的文本输入字段,该字段出现在 DOM 中的密码字段之前。 As the browser is the last instance and you can not control it.由于浏览器是最后一个实例,您无法控制它。

This readonly-fix above worked for me.上面的这个只读修复对我有用。

The solution for Chrome is to add autocomplete="new-password" to the input type password. Chrome 的解决方案是在输入类型密码中添加autocomplete="new-password" Please check the example below.请检查下面的示例。

Example:例子:

<form name="myForm"" method="post">
   <input name="user" type="text" />
   <input name="pass" type="password" autocomplete="new-password" />
   <input type="submit">
</form>

Chrome always autocomplete the data if it finds a box of type password , just enough to indicate for that box autocomplete = "new-password" .如果 Chrome 找到一个密码类型的框,它总是自动完成数据,这足以表明该框autocomplete = "new-password"

This works well for me.这对我很有效。

Note: make sure with F12 that your changes take effect.注意:使用F12确保您的更改生效。 Many times, browsers save the page in the cache, and this gave me a bad impression that it did not work, but the browser did not actually bring the changes.很多时候,浏览器将页面保存在缓存中,这给我一个不好的印象,它不起作用,但浏览器并没有真正带来变化。

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

This will work in Internet Explorer and Mozilla Firefox.这适用于 Internet Explorer 和 Mozilla Firefox。 The downside is that it is not XHTML standard.缺点是它不是 XHTML 标准。

As others have said, the answer is autocomplete="off" .正如其他人所说,答案是autocomplete="off"

However, I think it's worth stating why it's a good idea to use this in certain cases as some answers to this and duplicate questions have suggested it's better not to turn it off.但是,我认为值得说明为什么在某些情况下使用它是一个好主意,因为对此的一些答案和重复的问题表明最好不要关闭它。

Stopping browsers storing credit card numbers shouldn't be left to users.停止浏览器存储信用卡号码不应该留给用户。 Too many users won't even realize it's a problem.太多的用户甚至不会意识到这是一个问题。

It's particularly important to turn it off on fields for credit card security codes.在信用卡安全码字段上将其关闭尤为重要。 As this page states:本页所述:

"Never store the security code ... its value depends on the presumption that the only way to supply it is to read it from the physical credit card, proving that the person supplying it actually holds the card." “永远不要存储安全码......它的价值取决于提供它的唯一方法是从实体信用卡中读取它的假设,证明提供它的人实际上持有卡。”

The problem is, if it's a public computer (cyber cafe, library, etc.), it's then easy for other users to steal your card details, and even on your own machine a malicious website could steal autocomplete data .问题是,如果它是公共计算机(网吧、图书馆等),那么其他用户很容易窃取您的卡详细信息,甚至在您自己的计算机上,恶意网站也可能窃取自动完成数据

Always working solution始终有效的解决方案

I've solved the endless fight with Google Chrome with the use of random characters.我已经通过使用随机字符解决了与谷歌浏览器的无休止的斗争。 When you always render autocomplete with random string, it will never remember anything.当你总是用随机字符串渲染自动完成时,它永远不会记住任何东西。

<input name="name" type="text" autocomplete="rutjfkde">

Hope that it will help to other people.希望它对其他人有所帮助。

Update 2022: 2022 年更新:

Chrome made this improvement: autocomplete="new-password" which will solve it but I am not sure, if Chrome change it again to different functionality after some time. Chrome 进行了这项改进: autocomplete="new-password"可以解决它,但我不确定 Chrome 是否会在一段时间后再次将其更改为不同的功能。

I'd have to beg to differ with those answers that say to avoid disabling auto-complete.我不得不乞求与那些说避免禁用自动完成的答案不同。

The first thing to bring up is that auto-complete not being explicitly disabled on login form fields is a PCI-DSS fail.首先要提出的是,未在登录表单字段上明确禁用自动完成是 PCI-DSS 失败。 In addition, if a users' local machine is compromised then any autocomplete data can be trivially obtained by an attacker due to it being stored in the clear.此外,如果用户的本地计算机遭到破坏,那么任何自动完成数据都可以被攻击者轻易获得,因为它是以明文形式存储的。

There is certainly an argument for usability, however there's a very fine balance when it comes to which form fields should have autocomplete disabled and which should not.可用性肯定存在争议,但是在哪些表单字段应该禁用自动完成功能和哪些不应该禁用时,存在一个非常好的平衡。

Three options:三个选项:

First:第一的:

<input type='text' autocomplete='off' />

Second:第二:

<form action='' autocomplete='off'>

Third (JavaScript code):第三(JavaScript代码):

$('input').attr('autocomplete', 'off');

This works for me.这对我有用。

<input name="pass" type="password" autocomplete="new-password" />

We can also use this strategy in other controls like text, select etc我们也可以在文本、选择等其他控件中使用此策略

In addition to此外

autocomplete="off"

Use利用

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

for the inputs that you do not want them to remember form data ( username , password , etc.) as shown below:对于您不希望他们记住表单数据的输入( usernamepassword等),如下所示:

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

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

Hope this helps.希望这可以帮助。

On a related or actually, on the completely opposite note -在一个相关的或实际上,在完全相反的音符上——

"If you're the user of the aforementioned form and want to re-enable the autocomplete functionality, use the 'remember password' bookmarklet from this bookmarklets page . It removes all autocomplete="off" attributes from all forms on the page. Keep fighting the good fight!" “如果您是上述表单的用户并希望重新启用自动完成功能,请使用此书签页面中的“记住密码”书签。它会从页面上的所有表单中删除所有autocomplete="off"属性。保留打好仗!”

Just set autocomplete="off" .只需设置autocomplete="off" There is a very good reason for doing this: You want to provide your own autocomplete functionality!这样做有一个很好的理由:您想提供自己的自动完成功能!

None of the solutions worked for me in this conversation.在这次谈话中,没有一个解决方案对我有用。

I finally figured out a pure HTML solution that doesn't require any JavaScript , works in modern browsers (except Internet Explorer; there had to at least be one catch, right?), and does not require you to disable autocomplete for the entire form.我终于想出了一个不需要任何 JavaScript纯 HTML 解决方案,可以在现代浏览器中运行(Internet Explorer 除外;必须至少有一个问题,对吧?),并且不需要您禁用整个表单的自动完成功能.

Simply turn off autocomplete on the form and then turn it ON for any input you wish it to work within the form.只需关闭form上的自动完成功能,然后为您希望它在表单中工作的任何input打开它。 For example:例如:

<form autocomplete="off">
    <!-- These inputs will not allow autocomplete and Chrome
         won't highlight them yellow! -->
    <input name="username"  />
    <input name="password" type="password" />
    <!-- This field will allow autocomplete to work even
         though we've disabled it on the form -->
    <input name="another_field" autocomplete="on" />
</form>

I've been trying endless solutions, and then I found this:我一直在尝试无尽的解决方案,然后我发现了这个:

Instead of autocomplete="off" just simply use autocomplete="false"而不是autocomplete="off"只需简单地使用autocomplete="false"

As simple as that, and it works like a charm in Google Chrome as well!就这么简单,它在谷歌浏览器中也像一个魅力!

We did actually use sasb 's idea for one site.我们确实在一个站点上使用了 sasb的想法。

It was a medical software web app to run a doctor's office.这是一个运行医生办公室的医疗软件网络应用程序。 However, many of our clients were surgeons who used lots of different workstations, including semi-public terminals.然而,我们的许多客户都是外科医生,他们使用了许多不同的工作站,包括半公共终端。 So, they wanted to make sure that a doctor who doesn't understand the implication of auto-saved passwords or isn't paying attention can't accidentally leave their login information easily accessible.因此,他们希望确保不了解自动保存密码的含义或没有注意的医生不会意外地让他们的登录信息易于访问。

Of course, this was before the idea of private browsing that is starting to be featured in Internet Explorer 8, Firefox 3.1, etc. Even so, many physicians are forced to use old school browsers in hospitals with IT that won't change.当然,这是在 Internet Explorer 8、Firefox 3.1 等开始出现隐私浏览的想法之前。即便如此,许多医生仍被迫在 IT 不会改变的医院中使用老式浏览器。

So, we had the login page generate random field names that would only work for that post.因此,我们让登录页面生成仅适用于该帖子的随机字段名称。 Yes, it's less convenient, but it's just hitting the user over the head about not storing login information on public terminals.是的,它不太方便,但它只是让用户头疼的是不在公共终端上存储登录信息。

I think autocomplete=off is supported in HTML 5.我认为 HTML 5 支持autocomplete=off

Ask yourself why you want to do this though - it may make sense in some situations but don't do it just for the sake of doing it.问问自己为什么要这样做——在某些情况下这可能是有道理的,但不要仅仅为了这样做而这样做。

It's less convenient for users and not even a security issue in OS X (mentioned by Soren below).这对用户来说不太方便,甚至在 OS X 中也没有安全问题(下面由 Soren 提到)。 If you're worried about people having their passwords stolen remotely - a keystroke logger could still do it even though your app uses autcomplete=off .如果您担心人们的密码被远程盗取 - 即使您的应用程序使用autcomplete=off ,按键记录器仍然可以做到这一点。

As a user who chooses to have a browser remember (most of) my information, I'd find it annoying if your site didn't remember mine.作为选择让浏览器记住(大部分)我的信息的用户,如果您的网站不记得我的信息,我会觉得很烦人。

The best solution:最佳解决方案:

Prevent autocomplete username (or email) and password:防止自动完成用户名(或电子邮件)和密码:

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

Prevent autocomplete a field:防止自动完成字段:

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

Explanation: autocomplete continues work in <input> , autocomplete="off" does not work, but you can change off to a random string, like nope .说明: autocomplete<input>中继续工作, autocomplete="off"不起作用,但您可以将off更改为随机字符串,例如nope

Works in:工作于:

  • Chrome: 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 and 64铬:49、50、51、52、53、54、55、56、57、58、59、60、61、62、63 和 64

  • Firefox: 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 and 58火狐:44、45、46、47、48、49、50、51、52、53、54、55、56、57和58

Use a non-standard name and id for the fields, so rather than "name" have "name_".为字段使用非标准名称和 ID,因此“name”而不是“name_”。 Browsers will then not see it as being the name field.浏览器将不会将其视为名称字段。

The best part about it is that you can do this to some, but not all, fields and it will autocomplete some, but not all fields.最好的部分是您可以对某些(但不是全部)字段执行此操作,它会自动完成一些(但不是所有)字段。

Adding autocomplete="off" is not going to cut it.添加autocomplete="off"不会削减它。

Change the input type attribute to type="search" .input类型属性更改为type="search"
Google doesn't apply auto-fill to inputs with a type of search. Google 不会对具有某种搜索类型的输入应用自动填充。

I just ran into this problem and tried several failures, but this one works for me (found on MDN ):我刚遇到这个问题并尝试了几次失败,但这一个对我有用(在MDN上找到):

In some cases, the browser will keep suggesting autocompletion values even if the autocomplete attribute is set to off.在某些情况下,即使自动完成属性设置为关闭,浏览器也会继续建议自动完成值。 This unexpected behavior can be quite puzzling for developers.这种意想不到的行为可能会让开发人员感到非常困惑。 The trick to really force the no-completion is to assign a random string to the attribute like so:真正强制不完成的技巧是为属性分配一个随机字符串,如下所示:

autocomplete="nope"

Adding the添加

autocomplete="off"

to the form tag will disable the browser autocomplete (what was previously typed into that field) from all input fields within that particular form.到表单标签将禁用该特定表单中所有input字段的浏览器自动完成(之前在该字段中键入的内容)。

Tested on:测试:

  • Firefox 3.5, 4 BETA火狐 3.5、4 测试版
  • Internet Explorer 8互联网浏览器 8
  • Chrome铬合金

So here is it:所以这里是:

 function turnOnPasswordStyle() { $('#inputpassword').attr('type', "password"); }
 <input oninput="turnOnPasswordStyle()" id="inputpassword" type="text">

In order to avoid the invalid XHTML, you can set this attribute using JavaScript.为了避免无效的 XHTML,您可以使用 JavaScript 设置此属性。 An example using jQuery:使用 jQuery 的示例:

<input type="text" class="noAutoComplete" ... />

$(function() {
    $('.noAutoComplete').attr('autocomplete', 'off');
});

The problem is that users without JavaScript will get the autocomplete functionality.问题是没有 JavaScript 的用户将获得自动完成功能。

This is a security issue that browsers ignore now.这是浏览器现在忽略的安全问题。 Browsers identify and store content using input names, even if developers consider the information to be sensitive and should not be stored.浏览器使用输入名称来识别和存储内容,即使开发人员认为该信息是敏感信息并且不应存储。

Making an input name different between 2 requests will solve the problem (but will still be saved in browser's cache and will also increase browser's cache).使两个请求之间的输入名称不同将解决问题(但仍会保存在浏览器的缓存中,也会增加浏览器的缓存)。

Asking the user to activate or deactivate options in their browser's settings is not a good solution.要求用户在浏览器设置中激活或停用选项并不是一个好的解决方案。 The issue can be fixed in the backend.该问题可以在后端解决。

Here's the fix.这是修复。 All autocomplete elements are generated with a hidden input like this:所有自动完成元素都是使用隐藏输入生成的,如下所示:

<?php $r = md5(rand() . microtime(TRUE)); ?>
<form method="POST" action="./">
    <input type="text" name="<?php echo $r; ?>" />
    <input type="hidden" name="__autocomplete_fix_<?php echo $r; ?>" value="username" />
    <input type="submit" name="submit" value="submit" />
</form>

The server then processes the post variables like this: ( Demo )然后服务器像这样处理 post 变量:( Demo )

foreach ($_POST as $key => $val) {
    $newKey = preg_replace('~^__autocomplete_fix_~', '', $key, 1, $count);
    if ($count) {
        $_POST[$val] = $_POST[$newKey];
        unset($_POST[$key], $_POST[$newKey]);
    }
}

The value can be accessed as usual可以照常访问该值

echo $_POST['username'];

And the browser won't be able to suggest information from the previous request or from previous users.并且浏览器将无法建议来自先前请求或先前用户的信息。

This will continue to work even if browsers update their techniques to ignore/respect autocomplete attributes.即使浏览器更新他们的技术以忽略/尊重自动完成属性,这将继续有效。

I can't believe this is still an issue so long after it's been reported.我不敢相信这在被报道这么久之后仍然是一个问题。 The previous solutions didn't work for me, as Safari seemed to know when the element was not displayed or off-screen, however the following did work for me:以前的解决方案对我不起作用,因为Safari似乎知道元素何时不显示或不在屏幕上,但是以下解决方案对我有用:

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

如果只是autocomplete="off"不起作用,也试试这些:

autocorrect="off" autocapitalize="off" autocomplete="off"

None of the hacks mentioned here worked for me in Chrome.此处提到的所有技巧都不适用于 Chrome。 There's a discussion of the issue here: https://code.google.com/p/chromium/issues/detail?id=468153#c41这里讨论了这个问题: https ://code.google.com/p/chromium/issues/detail?id=468153#c41

Adding this inside a <form> works (at least for now):<form>中添加它是有效的(至少现在是这样):

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

You may use it in input .您可以在input中使用它。

For example;例如;

<input type=text name="test" autocomplete="off" />

Many modern browsers do not support autocomplete="off" for login fields anymore.许多现代浏览器不再支持登录字段的 autocomplete="off"。 autocomplete="new-password" is wokring instead, more information MDN docs autocomplete="new-password"是 wokring,更多信息MDN 文档

Things had changed now as I tried it myself old answers no longer work.现在事情发生了变化,因为我自己尝试了旧答案不再起作用。

Implementation that I'm sure it will work.我确信它会起作用的实施。 I test this in Chrome, Edge and Firefox and it does do the trick.我在 Chrome、Edge 和 Firefox 中对此进行了测试,它确实可以解决问题。 You may also try this and tell us your experience.您也可以试试这个并告诉我们您的经验。

set the autocomplete attribute of the password input element to "new-password"

<form autocomplete="off">
....other element
<input type="password" autocomplete="new-password"/>
</form>

This is according to MDN这是根据MDN

If you are defining a user management page where a user can specify a new password for another person, and therefore you want to prevent autofilling of password fields, you can use autocomplete="new-password"如果您正在定义用户管理页面,其中用户可以为另一个人指定新密码,因此您希望防止自动填充密码字段,您可以使用autocomplete="new-password"

This is a hint, which browsers are not required to comply with.这是一个提示,浏览器不需要遵守。 However modern browsers have stopped autofilling <input> elements with autocomplete="new-password" for this very reason.然而,出于这个原因,现代浏览器已经停止使用 autocomplete="new-password" 自动填充<input>元素。

Here's the perfect solution that will work in all browsers as of May 2021!这是自 2021 年 5 月起适用于所有浏览器的完美解决方案!

TL;DR TL;博士

Rename your input field names and field ids to something non-related like 'data_input_field_1' .将您的输入字段名称和字段 ID 重命名为不相关的名称,例如'data_input_field_1' Then add the &#8204;然后添加&#8204; character into the middle of your labels.字符到标签的中间。 This is a non-printing character, so you won't see it, but it tricks the browser into not recognizing the field as one needing auto-completing, thus no built-in auto-complete widget is shown!这是一个非打印字符,因此您不会看到它,但它会诱使浏览器不将该字段识别为需要自动完成的字段,因此不会显示内置的自动完成小部件!

The Details细节

Almost all browsers use a combination of the field's name, id, placeholder, and label to determine if the field belongs to a group of address fields that could benefit from auto-completion.几乎所有浏览器都使用字段名称、id、占位符和标签的组合来确定该字段是否属于可以从自动完成中受益的一组地址字段。 So if you have a field like <input type="text" id="address" name="street_address"> pretty much all browsers will interpret the field as being an address field.因此,如果您有一个像<input type="text" id="address" name="street_address">这样的字段,几乎所有浏览器都会将该字段解释为地址字段。 As such the browser will display its built-in auto-completion widget.因此,浏览器将显示其内置的自动完成小部件。 The dream would be that using the attribute autocomplete="off" would work, unfortunately, most browsers nowadays don't obey the request.梦想是使用属性autocomplete="off"会起作用,不幸的是,现在大多数浏览器都不服从请求。

So we need to use some trickery to get the browsers to not display the built-in autocomplete widget.所以我们需要使用一些技巧来让浏览器不显示内置的自动完成小部件。 The way we will do that is by fooling the browser into believing that the field is not an address field at all.我们将这样做的方法是欺骗浏览器,使其相信该字段根本不是地址字段。

Start by renaming the id and the name attributes to something that won't give away that you're dealing with address-related data.首先将idname属性重命名为不会泄露您正在处理与地址相关的数据的内容。 So rather than using <input type="text" id="city-input" name="city"> , use something like this instead <input type="text" id="input-field-3" name="data_input_field_3"> .因此,不要使用<input type="text" id="city-input" name="city"> ,而是使用类似的东西<input type="text" id="input-field-3" name="data_input_field_3"> . The browser doesn't know what data_input_field_3 represents.浏览器不知道 data_input_field_3 代表什么。 But you do.但你做了。

If possible, don't use placeholder text as most browsers will also take that into account.如果可能,不要使用占位符文本,因为大多数浏览器也会考虑到这一点。 If you have to use placeholder text, then you'll have to get creative and make sure you're not using any words relating to the address parameter itself (like City ).如果您必须使用占位符文本,那么您必须发挥创意并确保您没有使用任何与地址参数本身相关的词语(例如City )。 Using something like Enter location can do the trick.使用类似Enter location之类的东西可以解决问题。

The final parameter is the label attached to the field.最后一个参数是附加到字段的标签 However, if you're like me, you probably want to keep the label intact and display recognizable fields to your users like "Address", "City", "State", "Country".但是,如果您像我一样,您可能希望保持标签完整,并向您的用户显示可识别的字段,例如“地址”、“城市”、“州”、“国家”。 Well, great news: you can !好吧,好消息:你可以 The best way to achieve that is to insert a Zero-Width Non-Joiner Character , &#8204;实现这一点的最佳方法是插入一个零宽度非连接字符&#8204; , as the second character in the label. , 作为标签中的第二个字符。 So replacing <label>City</label> with <label>C&#8204;ity</label> .所以用 < <label>C&#8204;ity</label>替换<label>City</label> label> 。 This is a non-printing character, so your users will see City , but the browser will be tricked into seeing C ity and not recognize the field!这是一个非打印字符,因此您的用户会看到City ,但浏览器会被欺骗看到C ity而无法识别该字段!

Mission accomplished!任务完成! If all went well, the browser should not display the built-in address auto-completion widget on those fields anymore!如果一切顺利,浏览器不应再在这些字段上显示内置地址自动完成小部件!

Chrome isplanning to support this . Chrome计划支持这一点

For now the best suggestion is to use an input type that is rarely autocompleted.目前最好的建议是使用很少自动完成的输入类型。

Chrome discussion 铬讨论

<input type='search' name="whatever" />

To be compatible with Firefox, use normal autocomplete='off'要与 Firefox 兼容,请使用普通 autocomplete='off'

<input type='search' name="whatever" autocomplete='off' />

You can disable autocomplete if you remove the form tag.如果您删除form标签,您可以禁用自动完成功能。

The same was done by my bank and I was wondering how they did this.我的银行也这样做了,我想知道他们是怎么做到的。 It even removes the value that was already remembered by the browser after you remove the tag.它甚至会在您删除标签后删除浏览器已经记住的值。

您可以简单地将autocomplete="off"放在 HTML 字段中,如以下代码。

<input type="text" name="" value="" autocomplete="off" />

No fake inputs, no javascript!没有虚假输入,没有javascript!

There is no way to disable autofill consistently across browsers.无法跨浏览器一致地禁用自动填充。 I have tried all the different suggestions and none of them work in all browsers.我尝试了所有不同的建议,但没有一个适用于所有浏览器。 The only way is not using password input at all.唯一的方法是根本不使用密码输入。 Here's what I came up with:这是我想出的:

<style type="text/css">
    @font-face {
        font-family: 'PasswordDots';
        src: url('text-security-disc.woff') format('woff');
        font-weight: normal;
        font-style: normal;
    }

    input.password {
        font-family: 'PasswordDots' !important;
        font-size: 8px !important;
    }
</style>

<input class="password" type="text" spellcheck="false" />

Download: text-security-disc.woff下载: text-security-disc.woff

Here's how my final result looks like:这是我的最终结果:

密码掩码

The negative side effect is that it's possible to copy plain text from the input, though it should be possible to prevent that with some JS.负面影响是可以从输入中复制纯文本,尽管应该可以使用一些 JS 来防止这种情况。

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        try {
            $("input[type='text']").each(
                function(){
                   $(this).attr("autocomplete", "off");
                });
        }
        catch (e) {
        }
    });
</script>

This is what we called autocomplete of a textbox.这就是我们所说的文本框的自动完成。

在此处输入图像描述

We can disable autocomplete of a Textbox in two ways:我们可以通过两种方式禁用文本框的自动完成:

  1. By Browser Label按浏览器标签

  2. By Code按代码

    To disable in a browser, go to the setting要在浏览器中禁用,请转到设置

要在浏览器中禁用,请转到设置

转到高级设置并取消选中该复选框,然后还原。

Go to Advanced Settings and uncheck the checkbox and then Restore.转到高级设置并取消选中该复选框,然后还原。

If you want to disable in coding label you can do as follows -如果要在编码标签中禁用,可以执行以下操作 -

Using AutoCompleteType="Disabled" :使用AutoCompleteType="Disabled"

<asp:TextBox runat="server" ID="txt_userid" AutoCompleteType="Disabled"></asp:TextBox>

By Setting Form autocomplete="off" :通过设置表单autocomplete="off"

<asp:TextBox runat="server" ID="txt_userid" autocomplete="off"></asp:TextBox>

By Setting Form autocomplete="off" :通过设置表单autocomplete="off"

<form id="form1" runat="server" autocomplete="off">
    // Your content
</form>

By using code in the .cs page:通过使用 .cs 页面中的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        txt_userid.Attributes.Add("autocomplete", "off");
    }
}

By using jQuery通过使用 jQuery

<head runat="server">
    <title></title>

    <script src="Scripts/jquery-1.6.4.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#txt_userid').attr('autocomplete', 'off');
        });
    </script>

If your issue is having a password field being auto-completed, then you may find this useful...如果您的问题是自动完成密码字段,那么您可能会发现这很有用...

We had this issue in several areas of our site where the business wanted to re-query the user for their username and password and specifically did not want the password autofill to work for contractual reasons.我们在网站的几个区域中遇到了这个问题,企业希望重新向用户查询他们的用户名和密码,并且出于合同原因特别不希望密码自动填充起作用。 We found that the easiest way to do this is to put in a fake password field for the browser to find and fill while the real password field remains untouched.我们发现最简单的方法是输入一个假密码字段供浏览器查找和填写,而真正的密码字段保持不变。

<!-- 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" />

Note that in Firefox and IE, it was simply enough to put any input of type password before the actual one but Chrome saw through that and forced me to actually name the fake password input (by giving it an obvious password id) to get it to "bite".请注意,在 Firefox 和 IE 中,将任何类型密码的输入放在实际密码之前就足够了,但 Chrome 看穿了这一点,并迫使我实际命名假密码输入(通过给它一个明显的密码 id)来获得它“咬”。 I used a class to implement the style instead of using an embedded style so try that if the above doesn't work for some reason.我使用了一个类来实现样式而不是使用嵌入式样式,所以如果上述方法由于某种原因不起作用,请尝试一下。

The answer dsuess posted with the readonly was very clever and worked.用 readonly 发布的答案非常聪明并且有效。

But as I am using Bootstrap , the readonly input field was - until focused - marked with grey background.但是当我使用Bootstrap时,只读输入字段 - 直到聚焦 - 用灰色背景标记。 While the document loads, you can trick the browser by simply locking and unlocking the input.在文档加载时,您可以通过简单地锁定和解锁输入来欺骗浏览器。

So I had an idea to implement this into a jQuery solution:所以我有一个想法将它实现到一个 jQuery 解决方案中:

jQuery(document).ready(function () {
    $("input").attr('readonly', true);
    $("input").removeAttr('readonly');
});

My problem was mostly autofill with Chrome, but I think this is probably more problematic than autocomplete.我的问题主要是 Chrome 的自动填充,但我认为这可能比自动填充更成问题。

Trick: using a timer to reset the form and set the password fields to blank.技巧:使用计时器重置表单并将密码字段设置为空白。 The 100 ms duration seems to be the minimum for it to work. 100 毫秒的持续时间似乎是它工作的最小值。

$(document).ready(function() {
    setTimeout(function() {
        var $form = $('#formId');
        $form[0].reset();
        $form.find('INPUT[type=password]').val('');
    }, 100);
});

我使用这个TextMode="password" autocomplete="new-password"并在页面加载 aspx txtPassword.Attributes.Add("value", '');

Google Chrome ignores the autocomplete="off" attribute for certain inputs, including password inputs and common inputs detected by name. Google Chrome 会忽略某些输入的autocomplete="off"属性,包括密码输入和按名称检测到的常见输入。

For example, if you have an input with name address , then Chrome will provide autofill suggestions from addresses entered on other sites, even if you tell it not to :例如,如果您有一个名称为address的输入,那么即使您告诉它不要,Chrome 也会根据在其他网站上输入的地址提供自动填充建议:

<input type="string" name="address" autocomplete="off">

If you don't want Chrome to do that, then you can rename or namespace the form field's name:如果您不希望 Chrome 这样做,那么您可以重命名或命名表单字段的名称:

<input type="string" name="mysite_addr" autocomplete="off">

If you don't mind autocompleting values which were previously entered on your site, then you can leave autocomplete enabled.如果您不介意自动完成之前在您的网站上输入的值,那么您可以启用自动完成。 Namespacing the field name should be enough to prevent values remembered from other sites from appearing.字段名称的命名空间应该足以防止从其他站点记住的值出现。

<input type="string" name="mysite_addr" autocomplete="on">

It could be important to know that Firefox (I think only Firefox) uses a value called ismxfilled that basically forces autocomplete.重要的是要知道 Firefox(我认为只有 Firefox)使用一个名为ismxfilled的值,它基本上强制自动完成。

ismxfilled="0" for OFF ismxfilled="0"表示OFF

or或者

ismxfilled="1" for ON ismxfilled="1"ON

It doesn't seem to be possible to achieve this without using a combination client side and server side code.如果不使用客户端和服务器端代码的组合,似乎不可能实现这一点。

In order to make sure that the user must fill in the form every time without autocomplete I use the following techniques:为了确保用户每次都必须在没有自动完成的情况下填写表单,我使用了以下技术:

  1. Generate the form field names on the server and use hidden input fields to store those names, so that when submitted to the server the server side code can use the generated names to access the field values.在服务器上生成表单字段名称并使用隐藏的输入字段来存储这些名称,以便在提交到服务器时服务器端代码可以使用生成的名称来访问字段值。 This is to stop the user from having the option to auto populate the fields.这是为了阻止用户选择自动填充字段。

  2. Place three instances of each form field on the form and hide the first and last fields of each set using css and then disable them after page load using javascript.将每个表单字段的三个实例放在表单上,​​并使用 css 隐藏每组的第一个和最后一个字段,然后在使用 javascript 加载页面后禁用它们。 This is to prevent the browser from filling in the fields automatically.这是为了防止浏览器自动填写字段。

Here is a fiddle that demonstrates the javascript, css and html as described in #2 https://jsfiddle.net/xnbxbpv4/这是一个演示 #2 https://jsfiddle.net/xnbxbpv4/中描述的 javascript、css 和 html 的小提琴

javascript: javascript:

$(document).ready(function() {
    $(".disable-input").attr("disabled", "disabled");
});

css: CSS:

.disable-input {
  display: none;
}

html: html:

<form>
<input type="email" name="username" placeholder="username" class="disable-input">
<input type="email" name="username" placeholder="username">
<input type="email" name="username" placeholder="username" class="disable-input">
<br>
<input type="password" name="password" placeholder="password" class="disable-input">
<input type="password" name="password" placeholder="password">
<input type="password" name="password" placeholder="password" class="disable-input">
<br>
<input type="submit" value="submit">
</form>

Here is a rough example of what the server code using asp.net with razor would be to facilitate #1这是一个粗略的示例,说明使用带有剃刀的 asp.net 的服务器代码将有助于#1

model:模型:

public class FormModel
{
    public string Username { get; set; }
    public string Password { get; set; }
}

controller:控制器:

public class FormController : Controller
{
    public ActionResult Form()
    {
        var m = new FormModel();

        m.Username = "F" + Guid.NewGuid().ToString();
        m.Password = "F" + Guid.NewGuid().ToString();

        return View(m);
    }

    public ActionResult Form(FormModel m)
    {
        var u = Request.Form[m.Username];
        var p = Request.Form[m.Password];

        // todo: do something with the form values

        ...

        return View(m);
    }
}

view:看法:

@model FormModel

@using (Html.BeginForm("Form", "Form"))
{
    @Html.HiddenFor(m => m.UserName)
    @Html.HiddenFor(m => m.Password)

    <input type="email" name="@Model.Username" placeholder="username" class="disable-input">
    <input type="email" name="@Model.Username" placeholder="username">
    <input type="email" name="@Model.Username" placeholder="username" class="disable-input">
    <br>
    <input type="password" name="@Model.Password" placeholder="password" class="disable-input">
    <input type="password" name="@Model.Password" placeholder="password">
    <input type="password" name="@Model.Password" placeholder="password" class="disable-input">
    <br>
    <input type="submit" value="submit">
}

I tried almost all the answers, but the new version of Chrome is smart;我尝试了几乎所有的答案,但新版 Chrome 很聪明; if you write如果你写

autocomplete="randomstring" or autocomplete="rutjfkde"

it automatically converts it to它会自动将其转换为

autocomplete="off"

when the input control receives the focus.当输入控件获得焦点时。

So, I did it using jQuery, and my solution is as follows.所以,我用jQuery做的,我的解决方案如下。

$("input[type=text], input[type=number], input[type=email], input[type=password]").focus(function (e) {
    $(this).attr("autocomplete", "new-password");
})

This is the easiest and will do the trick for any number of controls you have on the form.这是最简单的,并且对于您在表单上拥有的任意数量的控件都有效。

<input autocomplete="off" aria-invalid="false" aria-haspopup="false" spellcheck="false" />

i find it works for me on all browsers.我发现它适用于所有浏览器。 When I make use of only the autocomplete it doesn't work except i combine all the attributes that you see.当我只使用自动完成功能时,它不起作用,除非我结合了您看到的所有属性。 Also i got the solution from google form input field我也从谷歌表单输入字段中得到了解决方案

Safari does not change its mind about autocomplete if you set autocomplete="off" dynamically from JavaScript.如果您从 JavaScript 动态设置autocomplete="off" ,Safari 不会改变它对自动完成的看法。 However, it would respect if you do that on per-field basis.但是,如果您在每个字段的基础上这样做,它会尊重。

$(':input', $formElement).attr('autocomplete', 'off');

The idea is to create an invisible field with the same name before the original one.这个想法是在原始字段之前创建一个具有相同名称的不可见字段。 That will make the browser auto populate the hidden field.这将使浏览器自动填充隐藏字段。

I use the following jQuery snippet:我使用以下 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
        ;
    });
};

And then just $('#login-form input').preventAutocomplete();然后只是$('#login-form input').preventAutocomplete();

尝试这个 :

<input type='text' autocomplete='off' />

To solve this problem, I have used some CSS tricks and the following works for me.为了解决这个问题,我使用了一些 CSS 技巧,以下对我有用。

input {
    text-security:disc;
    -webkit-text-security:disc;
    -mox-text-security:disc;
}

Please read this article for further detail.请阅读本文了解更多详情。

To prevent browser auto fill with the user's saved site login credentials, place a text and password input field at the top of the form with non empty values and style "position: absolute; top: -999px; left:-999px" set to hide the fields.为了防止浏览器自动填充用户保存的站点登录凭据,请在表单顶部放置一个文本和密码输入字段,其中包含非空值和样式“位置:绝对;顶部:-999px;左侧:-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>

It is important that a text field precede the password field.在密码字段之前有一个文本字段很重要。 Otherwise the auto fill may not be prevented in some cases.否则在某些情况下可能无法阻止自动填充。

It is important that the value of both the text and password fields not be empty, to prevent default values from being overwritten in some cases.重要的是文本和密码字段的值都不能为空,以防止在某些情况下覆盖默认值。

It is important that these two fields are before the "real" password type field(s) in the form.重要的是,这两个字段位于表单中“真实”密码类型字段之前。

For newer browsers that are html 5.3 compliant the autocomplete attribute value "new-password" should work.对于兼容 html 5.3 的较新浏览器,自动完成属性值“new-password”应该可以工作。

<form>
  <input type="text" name="username" value="" />
  <input type="password" name="password" value="" autocomplete="new-password" />
</form>

A combination of the two methods can be used to support both older and newer browsers.这两种方法的组合可用于支持较旧和较新的浏览器。

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

If you want to prevent the common browser plug-in LastPass from auto-filling a field as well, you can add the attribute data-lpignore="true" added to the other suggestions on this thread.如果您还想防止常见的浏览器插件LastPass自动填充字段,您可以将属性data-lpignore="true"添加到该线程的其他建议中。 Note that this doesn't only apply to password fields.请注意,这不仅适用于密码字段。

<input type="text" autocomplete="false" data-lpignore="true" />

I was trying to do this same thing a while back, and was stumped because none of the suggestions I found worked for me.不久前我试图做同样的事情,但因为我发现的任何建议都不适合我而感到难过。 Turned out it was LastPass .原来是LastPass

Most of the answers didn't help as the browser was simply ignoring them.大多数答案都没有帮助,因为浏览器只是忽略了它们。 (Some of them were not cross-browser compatible). (其中一些不是跨浏览器兼容的)。 The fix that worked for me is:对我有用的修复是:

<form autocomplete="off">
    <input type="text" autocomplete="new-password" />
    <input type="password" autocomplete="new-password" />
</form>

I set autofill="off" on the form tag and autofill="new-password" wherever the autofill was not necessary.我在表单标签上设置了 autofill="off",在不需要自动填充的地方设置了 autofill="new-password"。

Easy Hack轻松破解

Make input read-only将输入设为只读

<input type="text" name="name" readonly="readonly">

Remove read-only after timeout超时后删除只读

$(function() {
    setTimeout(function() {
        $('input[name="name"]').prop('readonly', false);
    }, 50);
});

As of Dec 2019:截至 2019 年 12 月:

Before answering this question let me say, I tried almost all the answers here on SO and from different forums but couldn't find a solution that works for all modern browsers and IE11.在回答这个问题之前让我说,我在 SO 和不同的论坛上尝试了几乎所有的答案,但找不到适用于所有现代浏览器和 IE11 的解决方案。

So here is the solution I found, and I believe it's not yet discussed or mentioned in this post.所以这是我找到的解决方案,我相信这篇文章还没有讨论或提到它。

According to Mozilla Dev Network(MDN) post about how to turn off form autocomplete根据 Mozilla Dev Network(MDN) post about how to turn off form autocomplete

By default, browsers remember information that the user submits through fields on websites.默认情况下,浏览器会记住用户通过网站上的字段提交的信息。 This enables the browser to offer autocompletion (that is, suggest possible completions for fields that the user has started typing in) or autofill (that is, pre-populate certain fields upon load)这使浏览器能够提供自动补全(即为用户已开始输入的字段建议可能的补全)或自动填充(即在加载时预填充某些字段)

On same article they discussed the usage of autocmplete property and its limitation.在同一篇文章中,他们讨论了autocmplete属性的用法及其限制。 As we know, not all browsers honor this attribute as we desire.正如我们所知,并非所有浏览器都如我们所愿地尊重这个属性。

Solution解决方案

So at the end of the article they shared a solution that works for all browsers including IE11+Edge.所以在文章的最后,他们分享了一个适用于包括 IE11+Edge 在内的所有浏览器的解决方案。 It is basically a jQuery plugin that do the trick.它基本上是一个可以解决问题的 jQuery 插件。 Here is the link to jQuery plugin and how it works.这是 jQuery 插件的链接及其工作原理。

Code snippet:代码片段:

$(document).ready(function () {        
    $('#frmLogin').disableAutoFill({
        passwordField: '.password'
    });
});

Point to notice in HTML is that password field is of type text and password class is applied to identify that field:在 HTML 中需要注意的是密码字段是文本类型, password类用于识别该字段:

<input id="Password" name="Password" type="text" class="form-control password">

Hope this would help someone.希望这会对某人有所帮助。

There are many answers but most of them are hacks or some kind of workaround.有很多答案,但其中大多数是黑客或某种解决方法。

There are three cases here.这里有三种情况。

Case I: If this is your standard login form.案例 I:如果这是您的标准登录表单。 Turning it off by any means is probably bad.以任何方式关闭它可能是不好的。 Think hard if you really need to do it.如果您真的需要这样做,请三思而后行。 Users are accustomed to browsers remembering and storing the passwords.用户习惯于浏览器记住和存储密码。 You shouldn't change that standard behaviour in most cases.在大多数情况下,您不应更改该标准行为。

In case you still want to do it, see Case III如果您仍然想这样做,请参阅案例 III

Case II: When this is not your regular login form but name or id attribute of inputs is not "like" email, login, username, user_name, password.情况二:当这不是您的常规登录表单但输入的nameid属性不是“喜欢”电子邮件、登录名、用户名、用户名、密码时。

Use利用

<input type="text" name="yoda" autocomplete="off">

Case III: When this is not your regular login form but name or id attribute of inputs is "like" email, login, username, user_name, password.案例三:当这不是您的常规登录表单但输入的nameid属性是“喜欢”电子邮件、登录名、用户名、用户名、密码时。

For example: login, abc_login, password, some_password, password_field.例如:login、abc_login、password、some_password、password_field。

All browsers come with password management features offering to remember them OR suggesting stronger passwords.所有浏览器都带有密码管理功能,可以记住它们或建议使用更强的密码。 That's how they do it.他们就是这样做的。

However, suppose you are an admin of a site and can create users and set their passwords.但是,假设您是网站管理员,可以创建用户并设置他们的密码。 In this case you wouldn't want browsers to offer these features.在这种情况下,您不希望浏览器提供这些功能。

In such cases, autocomplete="off" will not work.在这种情况下, autocomplete="off"将不起作用。 Use autocomplete="new-password"使用autocomplete="new-password"

<input type="text" name="yoda" autocomplete="new-password">

Helpful Link:有用的链接:

  1. https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion

在 Chrome 中,对于密码类型输入, autocomplete="new-password"是唯一对我有用的东西。

To disable the autocomplete of text in forms, use the autocomplete attribute of and elements.要禁用表单中文本的自动完成功能,请使用 和 元素的 autocomplete 属性。 You'll need the "off" value of this attribute.您需要此属性的“关闭”值。

This can be done in a for a complete form or for specific elements:这可以以完整形式或特定元素的形式完成:

  1. Add autocomplete="off" onto the element to disable autocomplete for the entire form.将 autocomplete="off" 添加到元素上以禁用整个表单的自动完成功能。
  2. Add autocomplete="off" for a specific element of the form.为表单的特定元素添加 autocomplete="off"。

form形式

<form action="#" method="GET" autocomplete="off">
</form>

input输入

<input type="text" name="Name" placeholder="First Name" autocomplete="off">

A workaround is not to insert the password field into the DOM before the user wants to change the password.一种解决方法是在用户想要更改密码之前不要将密码字段插入 DOM。 This may be applicable in certain cases:这可能适用于某些情况:

In our system we have a password field which in an admin page, so we must avoid inadvertently setting other users' passwords.在我们的系统中,我们在管理页面中有一个密码字段,因此我们必须避免无意中设置其他用户的密码。 The form has an extra checkbox that will toggle the password field visibility for this reason.由于这个原因,该表单有一个额外的复选框,它将切换密码字段的可见性。

So in this case, autofill from a password manager becomes a double problem, because the input won't even be visible to the user.所以在这种情况下,密码管理器的自动填充成为一个双重问题,因为用户甚至看不到输入。

The solution was to have the checkbox trigger whether the password field is inserted in the DOM, not just its visibility.解决方案是让复选框触发密码字段是否插入 DOM,而不仅仅是其可见性。

Pseudo implementation for AngularJS: AngularJS 的伪实现:

<input type="checkbox" ng-model="createPassword">
<input ng-if="changePassword" type="password">

You can use autocomplete = off in input controls to avoid auto completion您可以在输入控件中使用 autocomplete = off 来避免自动完成

For example:例如:

<input type=text name="test" autocomplete="off" />

if the above code doesn't works then try to add those attributes also如果上面的代码不起作用,那么也尝试添加这些属性

autocapitalize="off" autocomplete="off"

or或者

Change input type attribute to type="search" .将输入类型属性更改为type="search" Google doesn't apply auto-fill to inputs with a type of search. Google 不会对具有某种搜索类型的输入应用自动填充。

My solution is Change the text inputs type dynamically using angular js directive and it works like charm我的解决方案是使用 Angular js 指令动态更改文本输入类型,它就像魅力一样

first add 2 hidden text fields首先添加 2 个隐藏的文本字段

and just add a angular directive like this只需添加一个像这样的角度指令

 (function () {

    'use strict';

    appname.directive('changePasswordType', directive);

    directive.$inject = ['$timeout', '$rootScope',  '$cookies'];

    function directive($timeout,  $rootScope, $cookies) {
        var directive = {
            link: link,
            restrict: 'A'
        };

        return directive;

        function link(scope,element) {
            var process = function () {
                var elem =element[0];
                elem.value.length > 0 ? element[0].setAttribute("type", "password") :
                element[0].setAttribute("type", "text");
            }

            element.bind('input', function () {
                process();
            });

            element.bind('keyup', function () {
                process();
            });
        }
    }
})()

then use it in your text field where you need to prevent auto complete然后在需要防止自动完成的文本字段中使用它

    <input type="text" style="display:none">\\can avoid this 2 lines
    <input type="password" style="display:none">
    <input type="text"  autocomplete="new-password" change-password-type>

NB: dont forget to include jquery, and set type ="text" initially注意:不要忘记包含 jquery,并最初设置type ="text"

I wanted something that took the field management completely out of the browser's hands, so to speak.可以这么说,我想要一些完全脱离浏览器的字段管理的东西。 In this example, there's a single standard text input field to capture a password — no email, user name, etc...在这个例子中,有一个标准的文本输入字段来捕获密码——没有电子邮件、用户名等......

<input id='input_password' type='text' autocomplete='off' autofocus>

There's a variable named "input", set to be an empty string...有一个名为“input”的变量,设置为空字符串...

var input = "";

The field events are monitored by jQuery...现场事件由 jQuery 监控...

  1. On focus, the field content and the associated "input" variable are always cleared.在焦点上,字段内容和相关的“输入”变量总是被清除。
  2. On keypress, any alphanumeric character, as well as some defined symbols, are appended to the "input" variable, and the field input is replaced with a bullet character.在按键时,任何字母数字字符以及一些定义的符号都将附加到“输入”变量,并且字段输入被替换为项目符号字符。 Additionally, when the Enter key is pressed, and the typed characters (stored in the "input" variable) are sent to the server via Ajax.此外,当按下Enter键时,输入的字符(存储在“input”变量中)通过 Ajax 发送到服务器。 (See "Server Details" below.) (请参阅下面的“服务器详细信息”。)
  3. On keyup, the Home , End , and Arrow keys cause the "input" variable and field values to be flushed.在 keyup 上, HomeEndArrow键会导致“输入”变量和字段值被刷新。 (I could have gotten fancy with arrow navigation and the focus event, and used .selectionStart to figure out where the user had clicked or was navigating, but it's not worth the effort for a password field.) Additionally, pressing the Backspace key truncates both the variable and field content accordingly. (我本可以使用箭头导航和焦点事件,并使用.selectionStart来确定用户单击或导航的位置,但不值得为密码字段付出努力。)此外,按Backspace键会截断两者相应的变量和字段内容。

$("#input_password").off().on("focus", function(event) {
    $(this).val("");
    input = "";

}).on("keypress", function(event) {
    event.preventDefault();

    if (event.key !== "Enter" && event.key.match(/^[0-9a-z!@#\$%&*-_]/)) {
        $(this).val( $(this).val() + "•" );
        input += event.key;
    }
    else if (event.key == "Enter") {
        var params = {};
        params.password = input;

        $.post(SERVER_URL, params, function(data, status, ajax) {
            location.reload();
        });
    }

}).on("keyup", function(event) {
    var navigationKeys = ["Home", "End", "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"];
    if ($.inArray(event.key, navigationKeys) > -1) {
        event.preventDefault();
        $(this).val("");
        input = "";
    }
    else if (event.key == "Backspace") {
        var length = $(this).val().length - 1 > 0 ? $(this).val().length : 0;
        input = input.substring(0, length);
    }
});

Front-End Summary前端总结

In essence, this gives the browser nothing useful to capture.从本质上讲,这使浏览器无法捕获任何有用的信息。 Even if it overrides the autocomplete setting, and/or presents a dropdown with previously entered values, all it has is bullets stored for the field value.即使它覆盖了自动完成设置,和/或显示带有先前输入值的下拉列表,它所拥有的只是为字段值存储的项目符号。


Server Details (optional reading)服务器详细信息(可选阅读)

As shown above, JavaScript executes location.reload() as soon as the server returns a JSON response.如上所示,一旦服务器返回 JSON 响应,JavaScript 就会执行location.reload() (This logon technique is for access to a restricted administration tool. Some of the overkill, related to the cookie content, could be skipped for a more generalized implementation.) Here are the details: (此登录技术用于访问受限制的管理工具。一些与 cookie 内容相关的过度杀伤力可以跳过以获得更通用的实现。)以下是详细信息:

  • When a user navigates to the site, the server looks for a legitimate cookie.当用户导航到该站点时,服务器会寻找合法的 cookie。
  • If there isn't any cookie, the logon page is presented.如果没有任何 cookie,则会显示登录页面。 When the user enters a password and it is sent via Ajax, the server confirms the password and also checks to see if the user's IP address is in an Authorized IP address list.当用户输入密码并通过 Ajax 发送时,服务器会确认密码并检查用户的 IP 地址是否在授权 IP 地址列表中。
  • If either the password or IP address are not recognized, the server doesn't generate a cookie, so when the page reloads, the user sees the same logon page.如果无法识别密码或 IP 地址,则服务器不会生成 cookie,因此当页面重新加载时,用户会看到相同的登录页面。
  • If both the password and IP address are recognized, the server generates a cookie that has a ten-minute life span, and it also stores two scrambled values that correspond with the time-frame and IP address.如果密码和 IP 地址都被识别,服务器会生成一个有效期为十分钟的 cookie,它还会存储两个与时间范围和 IP 地址对应的加扰值。
  • When the page reloads, the server finds the cookie and verifies that the scrambled values are correct (ie, that the time-frame corresponds with the cookie's date and that the IP address is the same).当页面重新加载时,服务器会找到 cookie 并验证加扰的值是否正确(即,时间范围与 cookie 的日期相对应,并且 IP 地址是否相同)。
  • The process of authenticating and updating the cookie is repeated every time the user interacts with the server, whether they are logging in, displaying data, or updating a record.每次用户与服务器交互时,无论是登录、显示数据还是更新记录,都会重复验证和更新 cookie 的过程。
  • If at all times the cookie's values are correct, the server presents the full website (if the user is logging in) or fulfills whatever display or update request was submitted.如果 cookie 的值始终正确,则服务器会显示完整的网站(如果用户正在登录)或满足提交的任何显示或更新请求。
  • If at any time the cookie's values are not correct, the server removes the current cookie which then, upon reload, causes the logon page to be redisplayed.如果在任何时候 cookie 的值不正确,服务器会删除当前的 cookie,然后在重新加载时,重新显示登录页面。

Unfortunately, this option was removed in most browsers, so it is not possible to disable the password hint.不幸的是,该选项在大多数浏览器中已被删除,因此无法禁用密码提示。

Until today, I did not find a good solution to work around this problem.直到今天,我还没有找到解决这个问题的好方法。 What we have left now is to hope that one day this option will come back.我们现在剩下的就是希望有一天这个选项会回来。

I went through the same problem, today 09/10/2019 only solution I found was this我遇到了同样的问题,今天 09/10/2019 我找到的唯一解决方案是这个

Add autocomplete="off" into the form tag.将 autocomplete="off" 添加到表单标签中。

put 1 false inputs after opening form tag.在打开表单标签后放置 1 个错误输入。

<input id="username" style="display:none" type="text" name="fakeusernameremembered">

but it won't work on password type field, try但它不适用于密码类型字段,请尝试

<input type="text" oninput="turnOnPasswordStyle()" placeholder="Enter Password" name="password" id="password" required>

on script在脚本上

function turnOnPasswordStyle() {
    $('#password').attr('type', "password");
}

This is tested on Chrome-78, IE-44, Firefox-69这是在 Chrome-78、IE-44、Firefox-69 上测试的

The autofill functionality changes the value without selecting the field.自动填充功能在不选择字段的情况下更改值。 We could use that in our state management to ignore state changes before the select event.我们可以在状态管理中使用它来忽略 select 事件之前的状态变化。

An example in React : React中的一个例子:

import React, {Component} from 'react';

class NoAutoFillInput extends Component{

    constructor() {
        super();
        this.state = {
            locked: true
        }
    }

    onChange(event){
        if (!this.state.locked){
            this.props.onChange(event.target.value);
        }
    }

    render() {
        let props = {...this.props, ...{onChange: this.onChange.bind(this)}, ...{onSelect: () => this.setState({locked: false})}};
        return <input {...props}/>;
    }
}

export default NoAutoFillInput;

If the browser tries to fill the field, the element is still locked and the state is not affected.如果浏览器尝试填充该字段,则元素仍被锁定并且状态不受影响。 Now you can just replace the input field with a NoAutoFillInput component to prevent autofill:现在您可以用 NoAutoFillInput 组件替换输入字段以防止自动填充:

<div className="form-group row">
    <div className="col-sm-2">
        <NoAutoFillInput type="text" name="myUserName" className="form-control" placeholder="Username" value={this.state.userName} onChange={value => this.setState({userName: value})}/>
    </div>
    <div className="col-sm-2">
        <NoAutoFillInput type="password" name="myPassword" className="form-control" placeholder="Password" value={this.state.password} onChange={value => this.setState({password: value})}/>
    </div>
</div>

Of course, this idea could be used with other JavaScript frameworks as well.当然,这个想法也可以用于其他 JavaScript 框架。

None of the solutions I've found at this day bring a real working response.我今天找到的解决方案都没有带来真正的工作响应。

Solutions with an autocomplete attribute do not work.具有autocomplete属性的解决方案不起作用。

So, this is what I wrote for myself:所以,这是我为自己写的:

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

You should do this for every input field you want as a password type on your page.您应该对您希望在页面上作为密码类型的每个输入字段执行此操作。

And this works.这有效。

cheers干杯

Pretty sure this is the most generic solution as of today很确定这是迄今为止最通用的解决方案

This stops auto-complete and the popup suggestion box too.这也会停止自动完成和弹出建议框。

Intro介绍

Let's face it, autocomplete=off or new-password doesn't seem to work.面对现实吧, autocomplete=off 或 new-password 似乎不起作用。 It's should do but it doesn't and every week we discover something else has changed and the browser is filling a form out with more random garbage we don't want.它应该这样做,但它没有,每周我们都会发现其他事情发生了变化,并且浏览器正在填写一个表单,其中包含更多我们不想要的随机垃圾。 I've discovered a really simple solution that doesn't need autocomplete on sign in pages.我发现了一个非常简单的解决方案,不需要在登录页面上自动完成。

How to implement如何实施

Step 1).步骤1)。 Add the same function call to the onmousedown and onkeyup attributes for your username and password fields, making sure you give them an id AND note the code at the end of the function call.将相同的函数调用添加到用户名和密码字段的 onmousedown 和 onkeyup 属性,确保给它们一个 id 并在函数调用结束时记下代码。 md=mousedown and ku=keyup md=mousedown 和 ku=keyup

For the username field only add a value of &nbsp;对于用户名字段,只添加一个值&nbsp; as this prevents the form auto-filling on entry to the page.因为这可以防止表单在进入页面时自动填写。

For example:例如:

 <input type="text" value="&nbsp;" id="myusername" onmousedown="protectMe(this,'md')" onkeyup="protectMe(this,'ku')" />

Step 2).第2步)。 Include this function on the page在页面中包含此功能

 function protectMe(el,action){ // Remove the white space we injected on startup var v = el.value.trim(); // Ignore this reset process if we are typing content in // and only acknowledge a keypress when it's the last Delete // we do resulting in the form value being empty if(action=='ku' && v != ''){return;} // Fix double quote issue (in the form input) that results from writing the outerHTML back to itself v = v.replace(/"/g,'\\"'); // Reset the popup appearing when the form came into focus from a click by rewriting it back el.outerHTML=el.outerHTML; // Issue instruction to refocus it again, insert the value that existed before we reset it and then select the content. setTimeout("var d=document.getElementById('"+el.id+"'); d.value=\""+v+"\";d.focus();if(d.value.length>1){d.select();}",100); }

What does it do?它有什么作用?

  • Firstly it adds an &nbsp;首先它添加了一个&nbsp; space to the field so the browser tries to find details related to that but doesn't find anything, so that's auto-complete fixed字段的空间,以便浏览器尝试查找与该字段相关的详细信息但未找到任何内容,因此这是自动完成修复的
  • Secondly, when you click, the HTML is created again, cancelling out the popup and then the value is added back selected.其次,当您单击时,将再次创建 HTML,取消弹出窗口,然后将值添加回选中状态。
  • Finally, when you delete the string with the Delete button the popup usually ends up appearing again but the keyup check repeats the process if we hit that point.最后,当您使用 Delete 按钮删除字符串时,弹出窗口通常会再次出现,但如果我们达到该点,则 keyup 检查会重复该过程。

What are the issues?有哪些问题?

  • Clicking to select a character is a problem but for a sign in page most will forgive the fact it select all the text单击选择一个字符是一个问题,但对于登录页面,大多数人会原谅它选择所有文本的事实
  • Javascript does trim any inputs of blank spaces but you might want to do it too on the server side to be safe Javascript 确实会修剪空格的任何输入,但您可能希望在服务器端也这样做以确保安全

Can it be better?可以更好吗?

Yes probably.很可能是。 This is just something I tested and it satisfies my basic need but there might be more tricks to add or a better way to apply all of this.这只是我测试的东西,它满足了我的基本需求,但可能有更多的技巧可以添加或更好的方法来应用所有这些。

Tested browsers经测试的浏览器

Tested and working on latest Chrome, Firefox, Edge as of this post date截至本文发布日期,已在最新的 Chrome、Firefox、Edge 上测试并运行

I must have spent two days on this discussion before resorting to creating my own solution:在求助于创建自己的解决方案之前,我必须在这个讨论上花了两天时间:

First, if it works for the task, ditch the input and use a textarea .首先,如果它适用于该任务,请放弃input并使用textarea To my knowledge, autofill/autocomplete has no business in a textarea , which is a great start in terms of what we're trying to achieve.据我所知,自动填充/自动完成在textarea中没有任何业务,就我们想要实现的目标而言,这是一个很好的开始。 Now you just have to change some of the default behaviors of that element to make it act like an input .现在您只需更改该元素的一些默认行为,使其像input一样。

Next, you'll want to keep any long entries on the same line, like an input , and you'll want the textarea to scroll along the y-axis with the cursor.接下来,您将希望将任何长条目保留在同一行,例如input ,并且您希望textarea使用光标沿 y 轴滚动。 We also want to get rid of the resize box, since we're doing our best to mimic the behavior of an input , which doesn't come with a resize handle.我们还想摆脱调整大小框,因为我们正在尽最大努力模仿input的行为,它不带有调整大小句柄。 We achieve all of this with some quick CSS:我们通过一些快速的 CSS 实现了所有这些:

 #your-textarea { resize: none; overflow-y: hidden; white-space: nowrap; }

Finally, you'll want to make sure the pesky scrollbar doesn't arrive to wreck the party (for those particularly long entries), so make sure your text-area doesn't show it:最后,您需要确保讨厌的滚动条不会破坏聚会(对于那些特别长的条目),因此请确保您的文本区域不显示它:

 #your-textarea::-webkit-scrollbar { display: none; }

Easy peasy.十分简单。 We've had zero autocomplete issues with this solution.此解决方案的自动完成问题为零。

(It works in 2021 for Chrome (v88, 89, 90), Firefox, Brave, and Safari.) (它在 2021 年适用于 Chrome(v88、89、90)、Firefox、Brave 和 Safari。)

The old answers already written here will work with trial and error, but most of them don't link to any official documentation or what Chrome has to say on this matter.已经写在这里的旧答案可以反复试验,但大多数都没有链接到任何官方文档或 Chrome 在这个问题上必须说的内容。

The issue mentioned in the question is because of Chrome's autofill feature, and here is Chrome's stance on it in this bug link - Issue 468153: autocomplete=off is ignored on non-login INPUT elements, Comment 160问题中提到的问题是因为 Chrome 的自动填充功能,这是 Chrome 在此错误链接中的立场 - 问题 468153:autocomplete=off 在非登录 INPUT 元素上被忽略,评论 160

To put it simply, there are two cases -简单来说,有两种情况——

  • [CASE 1] : Your input type is something other than password . [案例 1] :您的input类型不是password In this case, the solution is simple, and has three steps.在这种情况下,解决方案很简单,分为三个步骤。

  • Add the name attribute to inputname属性添加到input

  • name should not start with a value like email or username. name不应以电子邮件或用户名之类的值开头。 Otherwise Chrome still ends up showing the dropdown.否则 Chrome 仍然会显示下拉菜单。 For example, name="emailToDelete" shows the dropdown, but name="to-delete-email" doesn't.例如, name="emailToDelete"显示下拉列表,但name="to-delete-email"不显示。 The same applies for the autocomplete attribute.这同样适用于autocomplete属性。

  • Add the autocomplete attribute, and add a value which is meaningful for you, like new-field-name添加autocomplete属性,并添加一个对您有意义的值,例如new-field-name

It will look like this, and you won't see the autofill for this input again for the rest of your life -它看起来像这样,在你的余生中你不会再看到这个输入的自动填充 -

  <input type="text/number/something-other-than-password" name="x-field-1" autocomplete="new-field-1" />
  • [CASE 2] : input type is password 【案例2】input typepassword
  • Well, in this case, irrespective of your trials, Chrome will show you the dropdown to manage passwords / use an already existing password.好吧,在这种情况下,无论您进行何种试验,Chrome 都会向您显示管理密码/使用现有密码的下拉菜单。 Firefox will also do something similar, and same will be the case with all other major browsers. Firefox 也会做类似的事情,所有其他主要浏览器也是如此。 2 2
  • In this case, if you really want to stop the user from seeing the dropdown to manage passwords / see a securely generated password, you will have to play around with JS to switch input type, as mentioned in the other answers of this question.在这种情况下,如果您真的想阻止用户查看管理密码的下拉菜单/查看安全生成的密码,您将不得不使用 JS 来切换输入类型,如本问题的其他答案中所述。

2 Detailed MDN documentation on turning off autocompletion - How to turn off form autocompletion 2关于关闭自动完成的详细 MDN 文档 - 如何关闭表单自动完成

With regards to Internet Explorer 11, there is a security feature in place that can be used to block autocomplete.对于 Internet Explorer 11,有一个可用于阻止自动完成的安全功能。

It works like this:它是这样工作的:

Any form input value that is modified in JavaScript after the user has already entered it is flagged as ineligible for autocomplete.在用户输入之后在 JavaScript 中修改的任何表单输入值都被标记为不符合自动完成的条件。

This feature is normally used to protect users from malicious websites that want to change your password after you enter it or the like.此功能通常用于保护用户免受恶意网站的侵害,这些网站希望在您输入密码后更改您的密码等。

However, you could insert a single special character at the beginning of a password string to block autocomplete.但是,您可以在密码字符串的开头插入一个特殊字符来阻止自动完成。 This special character could be detected and removed later on down the pipeline.稍后可以检测到此特殊字符并将其删除。

autocomplete = 'off' 对我不起作用,无论如何我将输入字段的 value 属性设置为一个空格,即<input type='text' name='username' value=" ">将默认输入字符设置为一个空格,由于用户名是空白的,所以密码也被清除了。

None of the provided answers worked on all the browsers I tested.提供的答案都不适用于我测试的所有浏览器。 Building on already provided answers, this is what I ended up with, (tested) on Chrome 61 , Microsoft Edge 40 (EdgeHTML 15), IE 11 , Firefox 57 , Opera 49 and Safari 5.1 .基于已经提供的答案,这就是我在Chrome 61Microsoft Edge 40 (EdgeHTML 15)、 IE 11Firefox 57Opera 49Safari 5.1上得到的结果(测试)。 It is wacky as a result of many trials;由于许多试验,它很古怪; however it does work for me.但是它确实对我有用。

<form autocomplete="off">
    ...
    <input type="password" readonly autocomplete="off" id="Password" name="Password" onblur="this.setAttribute('readonly');" onfocus="this.removeAttribute('readonly');" onfocusin="this.removeAttribute('readonly');" onfocusout="this.setAttribute('readonly');" />
    ...
</form> 

<script type="text/javascript">
    $(function () {           
        $('input#Password').val('');
        $('input#Password').on('focus', function () {
        if (!$(this).val() || $(this).val().length < 2) {
            $(this).attr('type', 'text');
        }
        else {
            $(this).attr('type', 'password');
        }
    });
    $('input#Password').on('keyup', function () {
        if (!$(this).val() || $(this).val().length < 2) {
            $(this).attr('type', 'text');
        }
        else {
            $(this).attr('type', 'password');
        }
    });
    $('input#Password').on('keydown', function () {
        if (!$(this).val() || $(this).val().length < 2) {
            $(this).attr('type', 'text');
        }
        else {
            $(this).attr('type', 'password');
        }
    });
</script>

Fixed.固定的。 Just need to add above real input field只需在真实输入字段上方添加

https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion - MDN https://medium.com/paul-jaworski/turning-off-autocomplete-in-chrome-ee3ff8ef0908 - medium tested on EDGE, Chrome(latest v63), Firefox Quantum (57.0.4 64-бит), Firefox(52.2.0) fake fields are a workaround for chrome/opera autofill getting the wrong fields https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion - MDN https://medium.com/paul-jaworski/turning-off-autocomplete-in-chrome-ee3ff8ef0908 - 中等在 EDGE、Chrome(最新 v63)、Firefox Quantum (57.0.4 64-бит)、Firefox(52.2.0) 上测试,假字段是 chrome/opera 自动填充获取错误字段的解决方法

 const fakeInputStyle = {opacity: 0, float: 'left', border: 'none', height: '0', width: '0'}

 <input type="password" name='fake-password' autoComplete='new-password' tabIndex='-1' style={fakeInputSyle} />

<TextField
  name='userName'
  autoComplete='nope'
  ... 
/>

<TextField
      name='password'
      autoComplete='new-password'
      ... 
    />

I was able to stop Chrome 66 from autofilling by adding two fake inputs and giving them position absolute:我可以通过添加两个假输入并给它们绝对位置来阻止 Chrome 66 自动填充:

<form style="position: relative">
  <div style="position: absolute; top: -999px; left: -999px;">
    <input name="username" type="text" />
    <input name="password" type="password" />
  </div>
  <input name="username" type="text" />
  <input name="password" type="password" />

At first, I tried adding display:none;起初,我尝试添加display:none; to the inputs but Chrome ignored them and autofilled the visible ones.到输入,但 Chrome 忽略它们并自动填充可见的。

This worked for me like a charm.这对我来说就像一个魅力。

  1. Set the autocomplete attribute of the form to off将表单的自动完成属性设置为关闭
  2. Add a dummy input field and set its attribute also to off.添加一个虚拟输入字段并将其属性也设置为关闭。
 <form autocomplete="off"> <input type="text" autocomplete="off" style="display:none"> </form>

I'v solved putting this code after page load:我已经解决了在页面加载后放置此代码:

<script>
var randomicAtomic = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
  $('input[type=text]').attr('autocomplete',randomicAtomic);
</script>

The simplest answer is最简单的答案是

<input autocomplete="on|off">

But keep in mind the browser support.但请记住浏览器支持。 Currently, autocomplete attribute is supported by目前,自动完成属性支持

Chrome 17.0 & latest IE 5.0 & latest Chrome 17.0 和最新 IE 5.0 和最新
Firefox 4.0 & latest Firefox 4.0 和最新版本
Safari 5.2 & latest Safari 5.2 和最新版本
Opera 9.6 & latest Opera 9.6 及最新版本

My solution with jQuery.我使用 jQuery 的解决方案。 It may not be 100% reliable, but it works for me.它可能不是 100% 可靠的,但它对我有用。 The idea is described in code annotations.这个想法在代码注释中描述。

 /** * Prevent fields autofill for fields. * When focusing on a text field with autocomplete (with values: "off", "none", "false") we replace the value with a new and unique one (here it is - "off-forced-[TIMESTAMP]"), * the browser does not find this type of autocomplete in the saved values and does not offer options. * Then, to prevent the entered text from being saved in the browser for a our new unique autocomplete, we replace it with the one set earlier when the field loses focus or when user press Enter key. * @type {{init: *}} */ var PreventFieldsAutofill = (function () { function init () { events.onPageStart(); } var events = { onPageStart: function () { $(document).on('focus', 'input[autocomplete="off"], input[autocomplete="none"], input[autocomplete="false"]', function () { methods.replaceAttrs($(this)); }); $(document).on('blur', 'input[data-prev-autocomplete]', function () { methods.returnAttrs($(this)); }); $(document).on('keydown', 'input[data-prev-autocomplete]', function (event) { if (event.keyCode == 13 || event.which == 13) { methods.returnAttrs($(this)); } }); $(document).on('submit', 'form', function () { $(this).find('input[data-prev-autocomplete]').each(function () { methods.returnAttrs($(this)); }); }); } }; var methods = { /** * Replace value of autocomplete and name attribute for unique and save the original value to new data attributes * @param $input */ replaceAttrs: function ($input) { var randomString = 'off-forced-' + Date.now(); $input.attr('data-prev-autocomplete', $input.attr('autocomplete')); $input.attr('autocomplete', randomString); if ($input.attr('name')) { $input.attr('data-prev-name', $input.attr('name')); $input.attr('name', randomString); } }, /** * Restore original autocomplete and name value for prevent saving text in browser for unique value * @param $input */ returnAttrs: function ($input) { $input.attr('autocomplete', $input.attr('data-prev-autocomplete')); $input.removeAttr('data-prev-autocomplete'); if ($input.attr('data-prev-name')) { $input.attr('name', $input.attr('data-prev-name')); $input.removeAttr('data-prev-name'); } } }; return { init: init } })(); PreventFieldsAutofill.init();
 .input { display: block; width: 90%; padding: 6px 12px; font-size: 14px; line-height: 1.42857143; color: #555555; background-color: #fff; background-image: none; border: 1px solid #ccc; border-radius: 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">Submit form</button> </p> </form>

只需尝试将值为“off”的属性自动完成设置为输入类型。

<input type="password" autocomplete="off" name="password" id="password" />
<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>
  1. Leave inputs with text type and hidden.将输入保留为文本类型并隐藏。
  2. On DOMContentLoaded, call a function that changes the types for password and display the fields, with a delay of 1s.在 DOMContentLoaded 上,调用更改密码类型并显示字段的函数,延迟为 1s。

 document.addEventListener('DOMContentLoaded', changeInputsType); function changeInputsType() { setTimeout(function() { $(/*selector*/).prop("type", "password").show(); }, 1000); }

After trying all solutions (some have worked partly, disabling autofill but not autocomplete, some did not work at all), I've found the best solution as of 2020 to be adding type="search" and autocomplete="off" to your input element.在尝试了所有解决方案之后(有些解决了部分问题,禁用了自动填充但没有自动完成,有些根本不起作用),我发现截至 2020 年的最佳解决方案是将 type="search" 和 autocomplete="off" 添加到您的输入元素。 Like this:像这样:

<input type="search" /> or <input type="search" autocomplete="off" />

Also make sure to have autocomplete="off" on the form element.还要确保在表单元素上有 autocomplete="off"。 This works perfectly and disables both autocomplete and autofill.这完美地工作并禁用自动完成和自动填充。

Also, if you're using type="email" or any other text type, you'll need to add autocomplete="new-email" and that will disable both perfectly.此外,如果您使用 type="email" 或任何其他文本类型,则需要添加 autocomplete="new-email" ,这将完全禁用两者。 same goes for type="password". type="password" 也是如此。 Just add a "new-" prefix to the autocomplete together with the type.只需将“new-”前缀与类型一起添加到自动完成中。 Like this:像这样:

<input type="email" autocomplete="new-email" />
<input type="password" autocomplete="new-password" />

I already posted a solution for this here: Disable Chrome Autofill creditcard我已经在这里发布了一个解决方案: 禁用 Chrome 自动填充信用卡

The workaround is to set the autocomplete attribute as "cc-csc" that value is the CSC of a credit card and that they are no allowed to store it!解决方法是将自动完成属性设置为“cc-csc”,该值是信用卡的CSC ,不允许存储它! (for now...) (目前...)

autocomplete="cc-csc"

Simply change the name attribute in your input element to something unique each time and it will never autocomplete again!每次只需将input元素中的name属性更改为唯一的,它就永远不会再自动完成了!

An example might be a time tic added at the end.一个例子可能是最后添加的时间 tic。 Your server would only need to parse the first part of the text name to retrieve the value back.您的服务器只需要解析文本name的第一部分来检索值。

<input type="password" name="password_{DateTime.Now.Ticks}" value="" />

I've been fighting this never-ending battle for ages now... And all tricks and hacks eventually stop working, almost as if browser devs are reading this question.多年来,我一直在与这场永无止境的战斗作斗争......所有的技巧和黑客最终都停止工作,就好像浏览器开发人员正在阅读这个问题一样。

I didn't want to randomize field names, touch the server-side code, use JS-heavy tricks and wanted to keep "hacks" to a minimum.我不想随机化字段名称、触摸服务器端代码、使用大量 JS 技巧,并希望将“hacks”保持在最低限度。 So here's what I came up with:所以这就是我想出的:

TL;DR use an input without a name or id at all! TL;DR使用完全没有nameidinput And track changes in a hidden field并在隐藏字段中跟踪更改

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

Works in all major browsers, obviously.显然,适用于所有主要浏览器。

PS Known minor issues: PS已知的小问题:

  1. you can't reference this field by id or name anymore.您不能再通过idname引用此字段。 But you can use CSS classes.但是你可以使用 CSS 类。 Or use $(#email').prev();或使用$(#email').prev(); in jQuery.在 jQuery 中。 Or come up with another solution (there's many).或者想出另一个解决方案(有很多)。
  2. oninput and onchange events do not fire when the textbox value is changed programmatically.以编程方式更改文本框值时,不会触发oninputonchange事件。 So modify your code accordingly to mirror changes in the hidden field too.因此,相应地修改您的代码以反映隐藏字段中的更改。

Since the behavior of autocomplete is pretty much unpredictable over different browsers and versions, my approach is a different one.由于autocomplete的行为在不同的浏览器和版本上几乎是不可预测的,所以我的方法是不同的。 Give all input elements for which you want to disable autofill the readonly attribute and only disable it on focus.为所有要禁用自动填充的输入元素提供readonly属性,并仅在焦点上禁用它。

  document.addEventListener('click', (e) => {
    readOnlys.forEach(readOnly => {
      if (e.target == readOnly) {
        readOnly.removeAttribute('readonly', '');
        readOnly.style.setProperty('pointer-events', 'none');
      } else {
        readOnly.setAttribute('readonly', '');
        readOnly.style.setProperty('pointer-events', 'auto');
      }
    });
  });
  document.addEventListener('keyup', (e) => {
    if (e.key == 'Tab') {
      readOnlys.forEach(readOnly => {
        if (e.target == readOnly) {
          readOnly.removeAttribute('readonly', '');
          readOnly.style.setProperty('pointer-events', 'none');
        } else {
          readOnly.setAttribute('readonly', '');
          readOnly.style.setProperty('pointer-events', 'auto');
        }
      });
    }
  });

If you want to make sure that users can still access the fields if they have disabled JS, you can set all readonly s initially via JS on page load.如果您想确保用户在禁用 JS 后仍然可以访问这些字段,您可以在页面加载时通过 JS 初始设置所有readonly You can still use the autocomplete attribute as a fallback.您仍然可以使用autocomplete属性作为后备。

You can add a name in attribute name how email address to your form and generate an email value.您可以在属性name中添加名称如何email地址到您的表单并生成电子邮件值。 For example:例如:

<form id="something-form">
  <input style="display: none" name="email" value="randomgeneratevalue"></input>
  <input type="password">
</form>

If you use this method, Google Chrome can't insert an autofill password.如果您使用此方法,Google Chrome 将无法插入自动填充密码。

Just autocomplete="off" list="autocompleteOff" in your input and work done for IE/Edge!只需在您的输入中输入autocomplete="off" list="autocompleteOff"并为 IE/Edge 完成工作! and for chrome add autocomplete="new password"并为 chrome 添加autocomplete="new password"

I was fighting to autocomplete for years.多年来,我一直在为自动完成而奋斗。 I've tried every single suggestion, and nothing worked for me.我已经尝试了每一个建议,但对我没有任何帮助。 Adding by jQuery 2 attributes worked well:通过 jQuery 2 属性添加效果很好:

$(document).ready( function () {
    setTimeout(function() {
        $('input').attr('autocomplete', 'off').attr('autocorrect', 'off');
    }, 10);
}); 

will result in HTML将导致 HTML

<input id="start_date" name="start_date" type="text" value="" class="input-small hasDatepicker" autocomplete="off" placeholder="Start date" autocorrect="off">

This works with Chrome 84.0.4147.105这适用于 Chrome 84.0.4147.105

1. Assign text type to password fields and add a class, eg "fakepasswordtype" 1.为密码字段分配文本类型并添加一个类,例如“fakepasswordtype”

<input type="text" class="fakepasswordtype" name="password1">
<input type="text" class="fakepasswordtype" name="password2">

2. Then use jQuery to change the type back to password the moment when first input is done 2.然后在第一次输入完成时使用jQuery将类型改回密码

jQuery(document).ready(function($) {
    $('.fakepasswordtype').on('input', function(e){
        $(this).prop('type', 'password');
    });
});

This stopped Chrome from its nasty ugly behavior from auto filling.这阻止了 Chrome 的自动填充令人讨厌的丑陋行为。

Chrome keeps trying to force autocomplete. Chrome 一直在尝试强制自动完成。 So there are a few things you need to do.因此,您需要做几件事。

The input field's attributes id and name need to not be recognizable.输入字段的属性idname不需要是可识别的。 So no values such as name, phone, address, etc.因此没有姓名、电话、地址等值。

The adjacent label or div also needs to not be recognizable.相邻的标签或 div 也需要不可识别。 However, you may wonder, how will the user know?但是,您可能想知道,用户如何知道? Well there is a fun little trick you can do with ::after using the content .好吧,您可以使用::after来做一个有趣的小技巧content You can set it to a letter.您可以将其设置为字母。

<label>Ph<span class='o'></span>ne</label>
<input id='phnbr' name='phnbr' type='text'>

<style>
  span.o {
    content: 'o';
  }
</style>

Very simple solution Just change the label name and fields name other than the more generic name eg: Name, Contact, Email.非常简单的解决方案只需更改标签名称和字段名称,而不是更通用的名称,例如:姓名、联系人、电子邮件。 Use "Mail To" instead of "Email".使用“邮寄至”而不是“电子邮件”。 Browsers ignore autocomplete off for these fields.浏览器会忽略这些字段的自动完成功能。

Define input's attribute type="text" along with autocomplete="off" works enough to turn off autocomplete for me.定义输入的属性 type="text" 以及 autocomplete="off" 足以为我关闭自动完成功能。

EXCEPT for type="password", I try switching the readonly attribute using JavaScript hooking on onfocus/onfocusout event from others' suggestions works fine BUT while the password field editing begin empty, autocomplete password comes again.除了 type="password" 之外,我尝试使用 JavaScript 挂钩 onfocus/onfocusout 事件从其他人的建议切换只读属性工作正常但是当密码字段编辑开始为空时,自动完成密码再次出现。

I would suggest switching the type attribute regarding length of password using JavaScript hooking on oninput event in addition to above workaround which worked well..除了上述工作良好的解决方法之外,我建议使用 JavaScript 挂钩 oninput 事件来切换关于密码长度的类型属性。

<input id="pwd1" type="text" autocomplete="off" oninput="sw(this)" readonly onfocus="a(this)" onfocusout="b(this)" />
<input id="pwd2" type="text" autocomplete="off" oninput="sw(this)" readonly onfocus="a(this)" onfocusout="b(this)" />

and JavaScript..和 JavaScript ..

function sw(e) {
    e.setAttribute('type', (e.value.length > 0) ? 'password' : 'text');
}
function a(e) {
    e.removeAttribute('readonly');
}
function b(e) {
    e.setAttribute('readonly', 'readonly');
}

Browser ignores autocomlete on readonly fields.浏览器忽略只读字段的自动完成。 This code makes all writable filds readonly till it's focused.这段代码使所有可写字段只读,直到它被聚焦。

 $(_=>{$('form[autocomplete=off] [name]:not([readonly])').map((i,t)=>$(t).prop('autocomplete',t.type=='password'?'new-password':'nope').prop('readonly',!0).one('focus',e=>$(t).prop('readonly',!1))); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form method="post" autocomplete="off"> <input name="user" id="user" required class="form-control" /> <label class="form-label" for="user">New User</label> <hr> <input type="password" name="pass" id="pass" minlength="8" maxlength="20" /> <label class="form-label" for="pass">New Passwort</label> </form>

I was looking arround to find a solution to this but none of them worked proberly on all browsers.我一直在寻找解决方案,但没有一个在所有浏览器上都能正常工作。

I tried autocomplete off, none, nope, new_input_64, (even some funny texts) because the autoComplete attribute expects a string to be passed, no matter what.我尝试关闭自动完成功能,无,不,new_input_64,(甚至是一些有趣的文本),因为 autoComplete 属性无论如何都需要传递一个字符串。

After many searches and attempts I found this solution.经过多次搜索和尝试,我找到了这个解决方案。

I changed all input types to text and added a bit of simple CSS code.我将所有input类型更改为text并添加了一些简单的 CSS 代码。

Here is the form:这是表格:

<form class="row gy-3">
  <div class="col-md-12">
    <label for="fullname" class="form-label">Full Name</label>
    <input type="text" class="form-control" id="fullname" name="fullname" value="">
  </div>
  <div class="col-md-6">
    <label for="username" class="form-label">User Name</label>
    <input type="text" class="form-control" id="username" name="username" value="">
  </div>
  <div class="col-md-6">
    <label for="email" class="form-label">Email</label>
    <input type="text" class="form-control" id="email" name="email" value="">
  </div>
  <div class="col-md-6">
    <label for="password" class="form-label">Password</label>
    <input type="text" class="form-control pswd" id="password" name="password" value="">
  </div>
  <div class="col-md-6">
    <label for="password" class="form-label">Confirm Password</label>
    <input type="text" class="form-control pswd" id="password" name="password" value="">
  </div>
  <div class="col-md-12">
    <label for="recaptcha" class="form-label">ReCaptcha</label>
    <input type="text" class="form-control" id="recaptcha" name="recaptcha" value="">
  </div>
</form>

The CSS code to hide password:隐藏密码的 CSS 代码:

.pswd {
    -webkit-text-security: disc;
}

Tested on Chrome, Opera, Edge.在 Chrome、Opera、Edge 上测试。

This will fix this problem这将解决这个问题

autocomplete="new-password"

2022 September 2022 年 9 月

Some browsers such as Google Chrome does not even care the value of autocomplete attribute.某些浏览器(例如 Google Chrome)甚至不关心autocomplete属性的值。 The best way to stop getting suggestions is changing the name attribute of the input to something that changes time to time停止获取建议的最佳方法将输入的名称属性更改为不时更改的内容

As of September 2022, browsers will not provide autocomplete for one time passwords自 2022 年 9 月起,浏览器将不再提供一次性密码的自动填充功能

So we can use otp as the field name.所以我们可以使用otp作为字段名。

<input name="otp"> <输入名称="otp">

you only need autocomplete attribute for this problem you canvisit this page for more information你只需要这个问题的自动完成属性你可以访问这个页面了解更多信息

<input type="text" name="foo" autocomplete="off" />

For React you can try to put this code either now under a form or above password input or between email and password inputs对于 React,您现在可以尝试将这段代码放在表单下方或密码输入上方,或者放在电子邮件和密码输入之间

export const HackRemoveBrowsersAutofill = () => (
  <>
    <input type="email" autoComplete="new-password" style={ { display: 'none' } } />
    <input type="password" autoComplete="new-password" style={ { display: 'none' } } />
  </>
)

One of the examples:例子之一:

<input type="email"/>
<HackRemoveBrowsersAutofill/>
<input type="password"/>
<!-- html -->
<input id="inputID" onfocus="this.readOnly = false;" onblur="this.readOnly = true;" />

More更多的

javascript and jQuery versions javascript 和 jQuery 版本

DO NOT USE JAVASCRIPT to fix this!!不要使用 JAVASCRIPT 来解决这个问题!!

Use HTML to address this problem first, in case browsers are not using scripts, fail to use your version of script, or have scripts turned off.首先使用 HTML 解决此问题,以防浏览器不使用脚本、无法使用您的脚本版本或关闭脚本。 Always layer scripting LAST on top of HTML.始终在 HTML 之上最后一层层编写脚本。

  1. For regular input form fields with "text" type attributes, add the autocomplete="off" on the element.对于具有“文本”类型属性的常规输入表单字段,请在元素上添加autocomplete="off" This may not work in modern HTML5 browsers due to user browser override settings but it takes care of many older browsers and stops the autocomplete drop down choices in most.由于用户浏览器覆盖设置,这在现代 HTML5 浏览器中可能不起作用,但它可以处理许多旧浏览器并在大多数情况下停止自动完成下拉选项。 Notice, I also include all the other autocomplete options that might be irritating to users, like autocapitalize="off" , autocorrect="off" , and spellcheck="false" .请注意,我还包括了所有其他可能会激怒用户的自动完成选项,例如autocapitalize="off"autocorrect="off"spellcheck="false" Many browser do not support these but they add extra "offs" of features that annoy data entry people.许多浏览器不支持这些,但它们添加了额外的“关闭”功能,这些功能会惹恼数据输入人员。 Note that Chrome for example ignores "off" if a users browsers are set with autofill enabled.请注意,例如,如果用户的浏览器设置为启用autofillChrome 会忽略“关闭” So, realize browser settings can override this attribute.所以,实现浏览器设置可以覆盖这个属性。

    <input type="text" id="myfield" name="myfield" size="20" value="" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" tabindex="0" placeholder="Enter something here..." title="Enter Something" aria-label="Something" required="required" aria-required="true" /> <input type="text" id="myfield" name="myfield" size="20" value="" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" tabindex="0 " placeholder="在这里输入一些东西..." title="输入一些东西" aria-label="Something" required="required" aria-required="true" />

  2. For username and password fields, there are some limited browser support for more specific attributes for autocomplete that trigger the "off" feature". autocomplete="username" on text inputs used for logins will force some browsers to reset and remove the autocomplete feature. autocomplete="new-password" will try and do the same for passwords using the "password" type input field. (see below). However, these are propriety to specific browsers, and will fail in most. Browsers that do not support these features include Internet Explorer 1-11, many Safari and Opera desktop browsers, and some version of iPhone and Android default browsers. But they provide additional power to try and force removal of autocomplete.对于用户名密码字段,对于触发“关闭”功能的自动完成更具体的属性,浏览器支持有限。用于登录的文本输入上的autocomplete autocomplete="username"将强制某些浏览器重置并删除自动完成功能。 autocomplete="new-password"将尝试使用“password”类型输入字段对密码执行相同操作。(见下文)。但是,这些对特定浏览器来说是适当的,并且在大多数情况下会失败。不支持这些的浏览器功能包括 Internet Explorer 1-11、许多 Safari 和 Opera 桌面浏览器以及某些版本的 iPhone 和 Android 默认浏览器。但它们提供了额外的功能来尝试强制删除自动完成功能。

    <input type="text" id="username" name="username" size="20" value="" autocomplete="username" autocapitalize="off" autocorrect="off" spellcheck="false" tabindex="0" placeholder="Enter username here..." title="Enter Username" aria-label="Username" required="required" aria-required="true" /> <input type="text" id="username" name="username" size="20" value="" autocomplete="username" autocapitalize="off" autocorrect="off" spellcheck="false" tabindex="0 " placeholder="在此处输入用户名..." title="输入用户名" aria-label="用户名" required="required" aria-required="true" />

    <input type="password" id="password" name="password" size="20" minlength="8" maxlength="12" pattern="(?=. \d)(?=. [az])(?=.*[AZ]).{8,12}" value="" autocomplete="new-password" autocapitalize="off" autocorrect="off" spellcheck="false" tabindex="0" placeholder="Enter password here..." title="Enter Password: (8-12) characters, must contain one number, one uppercase and lowercase letter" aria-label="Password" required="required" aria-required="true" /> <input type="password" id="password" name="password" size="20" minlength="8" maxlength="12" pattern="(?=. \d)(?=. [az]) (?=.*[AZ]).{8,12}" value="" autocomplete="new-password" autocapitalize="off" autocorrect="off" spellcheck="false" tabindex="0" placeholder="在此处输入密码..." title="输入密码:(8-12)个字符,必须包含一个数字,一个大写和小写字母" aria-label="Password" required="required" aria-required="true" />

NOW FOR A BETTER SOLUTION!现在寻求更好的解决方案!

Because of the splintered support for HTML5 by standards bodies and browsers, all these features fail in many modern browsers and most of the older ones.由于标准机构和浏览器对 HTML5 的支持分裂,所有这些功能在许多现代浏览器和大多数旧浏览器中都失败了。 So, some kids turn to scripting to fill in the holes.因此,一些孩子转向编写脚本来填补漏洞。 But that's lazy programming.但那是懒惰的编程。 Hiding or rewriting HTML using JavaScript makes things worse , as you now have layers upon layers of script dependencies PLUS the HTML patches.使用JavaScript 隐藏或重写 HTML 会使事情变得更糟,因为您现在拥有层层的脚本依赖以及 HTML 补丁。 Avoid this!避免这种情况!

The best way to permanently disable this feature in forms and fields is to simply create a custom "id"/"name" value each time on your 'input' elements using a unique value like a date or time concatenated to the field id and name attribute and make sure they match (eg "name=password_{date}").在表单和字段中永久禁用此功能的最佳方法是每次在“输入”元素上使用唯一值(例如与字段 ID 和名称连接的日期或时间)简单地创建自定义“id”/“name”值属性并确保它们匹配(例如“name=password_{date}”)。

<input type="text" id="password_20211013" name="password_20211013" />

This destroys the silly browser autocomplete choice algorythms sniffing for matched names that trigger past values for these fields.破坏了愚蠢的浏览器自动完成选择算法嗅探匹配的名称,触发这些字段的过去值。 Doing so, the browsers cannot match caches of previous form data with yours.这样做,浏览器无法将以前表单数据的缓存与您的缓存相匹配。 It will force "autocomplete" to be off, or show a blank list, and will not show any previous passwords ever again!它将强制“自动完成”关闭,或显示一个空白列表,并且不会再显示任何以前的密码! On the server side, you can create these custom input "named" fields and can still identify and extract the right field from the response coming from the user's browser by simply parsing the first part of the id/name.在服务器端,您可以创建这些自定义输入“命名”字段,并且仍然可以通过简单地解析 id/name 的第一部分来识别并从来自用户浏览器的响应中提取正确的字段。 For example "password_" or "username_", etc. When the field's "name" value comes in you simply parse for the first part ("password_{date}") and ignore the rest, then extract your value on the server.例如“password_”或“username_”等。当输入字段的“name”值时,您只需解析第一部分(“password_{date}”)并忽略其余部分,然后在服务器上提取您的值。 Easy!简单的!

I was having trouble on a form which included input fields for username and email with email being right after username.我在包含用户名和电子邮件输入字段的表单上遇到问题,其中电子邮件就在用户名之后。 Chrome would autofill my current username for the site into the email field and put focus on that field. Chrome 会自动将我当前的站点用户名填充到电子邮件字段中,并将重点放在该字段上。 This wasn't ideal as it was a form for adding new users to the web application.这并不理想,因为它是一种将新用户添加到 Web 应用程序的表单。

It seems that if you have two inputs one after the other where the first one has the term 'user name' or username in it's label or ID and the next one has the word email in it's label or ID chrome will autofill the email field.似乎如果您一个接一个地有两个输入,其中第一个在其标签或 ID 中包含术语“用户名”或用户名,而下一个在其标签或 ID 中包含单词电子邮件,chrome 将自动填充电子邮件字段。

What solved the issue for me was to change the ID's of the inputs to not include those words.为我解决这个问题的方法是将输入的 ID 更改为不包含这些单词。 I also had to set the text of the labels to an empty string and use a javascript settimeout function to change the label back to what it should be after 0.01s.我还必须将标签的文本设置为空字符串,并使用 javascript settimeout 函数将标签更改回 0.01 秒后的应有状态。

setTimeout(function () { document.getElementById('id_of_email_input_label').innerHTML = 'Email:'; }, 10);

If you are having this problem with any other input field being wrongly autofilled I'd try this method.如果您在错误地自动填充任何其他输入字段时遇到此问题,我会尝试这种方法。

To avoid autocomplete, add autocomplete="off" to your HTML input property.为避免自动完成,请将autocomplete="off"添加到您的 HTML输入属性。

Example:例子:

<input type="text" name="foo" autocomplete="off" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用 Web 表单字段/输入标签上的输入值? - How do you disable input value on web form field / input tag? 如何在角度表单字段上禁用浏览器自动完成功能? - How to disable browser autocomplete on angular form field? 如何在Web浏览器输入字段中禁用字典查找? - How do I disable dictionary lookup in web browser input field? 如何在Windows 10移动版上禁用Web表单输入上的浏览器自动完成/建议? - How to disable browser Autocomplete/Suggestions on web form input on Windows 10 mobile? 如何使用EditorFor禁用输入字段的自动完成功能? - How to disable input field's autocomplete with EditorFor? 禁用输入字段的自动完成/自动填充 - disable autocomplete / autofill for an input field 如何避免浏览器列出关于输入字段的建议(不一定与禁用自动完成/自动填充相同)? - How do I keep browser from listing suggestions for input-field (not necessarily the same as disabling autocomplete/autofill)? 如何在 React.js 中的 Input 元素上禁用浏览器自动完成 - How to disable browser autocomplete on Input element in React.js HTML输入表单禁用标签 - HTML input form disable tags 禁用按Tab键浏览Web表单中的下一个输入字段 - Disable press tab button to next input field in web form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM