Html 如何通过代码有选择地禁用浏览器文本输入中的自动填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1929486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:37:25  来源:igfitidea点击:

How to disable the autofill in browser text inputs selectively via code?

asp.nethtmlbrowserautofill

提问by holiveira

Is it possible to selectively disable the autofill feature in text fields using code?

是否可以使用代码有选择地禁用文本字段中的自动填充功能?

I'm developing my custom code in ASP.Net AJAX to search for the suggestion in the database and I would like to prevent the browser suggestions from appearing when the user starts typing in the textbox.

我正在 ASP.Net AJAX 中开发我的自定义代码以在数据库中搜索建议,我想防止在用户开始在文本框中键入时出现浏览器建议。

I'm looking for a solution that works in the most modern browsers (IE 7 & 8, Firefox, Safari and Chrome). It's is ok if the workaround is in Javascript.

我正在寻找一种适用于大多数现代浏览器(IE 7 和 8、Firefox、Safari 和 Chrome)的解决方案。如果解决方法是在 Javascript 中就可以了。

回答by Julien Vaslet

Look at the autocomplete HTML attribute (on form or input tags).

查看自动完成 HTML 属性(在表单或输入标签上)。

<form [...] autocomplete="off"> [...] </form>

W3C > Autocomplete attribute

W3C > 自动完成属性

Edit:

编辑:

Nowadays - from the comments - web browsers do not always respect the autocomplete tag defined behavior. This non-respect could be surrounded with a little of JavaScript code but you should think about you want to do before use this.

如今 - 从评论来看 - 网络浏览器并不总是尊重自动完成标签定义的行为。这种不尊重可能会被一些 JavaScript 代码包围,但在使用它之前你应该考虑你想要做什么。

First, fields will be filled during the page loading and will only be emptied once the page load. Users will question why the field has been emptied.

首先,字段将在页面加载期间填充,并且只有在页面加载后才会被清空。用户会质疑为什么该字段已被清空。

Second, this will reset other web browser mechanisms, like the autofill of a field when you go back to the previous page.

其次,这将重置其他 Web 浏览器机制,例如当您返回上一页时自动填充字段。

jQuery( function()
{
  $("input[autocomplete=off]").val( "" );
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form method="post" action="">
  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" id="username" name="username" value="John" autocomplete="off">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" id="password" name="password" value="qwerty" autocomplete="off">
  </div>
  <input type="submit" value="Login">
</form>

回答by Elliott

you can put it into your inputs:

你可以把它放到你的输入中:

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

or in jquery

或在 jquery

$(":input").attr("autocomplete","off"); 

回答by Sangram Nandkhile

There are 2 solutions for this problem. I have tested following code on Google Chrome v36. Recent Version of Google Chrome forces Autofill irrespective of the Autocomplete=off.Some of the previous hacks don't work anymore (34+ versions)

这个问题有2个解决方案。我在 Google Chrome v36 上测试了以下代码。最新版本的 Google Chrome 强制自动填充,而不管 .Autocomplete=off一些以前的黑客不再起作用(34+ 版本)

Solution 1:

解决方案1:

Put following code under under <form ..>tag.

将以下代码放在<form ..>标签下。

<form id="form1" runat="server" >
<input style="display:none" type="text" name="fakeusernameremembered"/>
<input style="display:none" type="password" name="fakepasswordremembered"/>

...

Read more

阅读更多

Solution 2:

解决方案2:

$('form[autocomplete="off"] input, input[autocomplete="off"]').each(function () {

                var input = this;
                var name = $(input).attr('name');
                var id = $(input).attr('id');

                $(input).removeAttr('name');
                $(input).removeAttr('id');

                setTimeout(function () {
                    $(input).attr('name', name);
                    $(input).attr('id', id);
                }, 1);
            });

It removes "name" and "id" attributes from elements and assigns them back after 1ms.

它从元素中删除“name”和“id”属性,并在 1 毫秒后将它们分配回来。

回答by Pete Kirkham

Adding an autocomplete=offattribute to the html inputelement should do that.

autocomplete=offhtmlinput元素添加属性应该可以做到这一点。

回答by Thelder

My workaround is to make the password field a normal textbox, then using jquery to turn it into password field on focus:

我的解决方法是使密码字段成为普通文本框,然后使用 jquery 将其转换为焦点密码字段:

asp:TextBox runat="server" ID="txtUsername" />
<asp:TextBox runat="server" ID="txtPassword" />
<script>
    $("#txtPassword").focus(function () {
        $("#txtPassword").prop('type', 'password');
    });
</script>

回答by George Dgebuadze

This should work in every browser

这应该适用于每个浏览器

    <script type="text/javascript">
        var c = document.getElementById("<%=TextBox1.ClientID %>");
        c.select =
        function (event, ui) 
        { this.value = ""; return false; }
    </script>

回答by Adam

Add the AutoCompleteType="Disabled"to your textbox

添加AutoCompleteType="Disabled"到您的文本框