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


当前回答

要防止浏览器自动填充用户保存的站点登录凭据,请在表单顶部放置一个文本和密码输入字段,该字段具有非空值,并设置样式“position:aabsolute;top:-999px;left:-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>

在密码字段之前有一个文本字段很重要。否则,在某些情况下可能无法阻止自动填充。

重要的是文本和密码字段的值不能为空,以防止在某些情况下覆盖默认值。

这两个字段必须在表单中的“真实”密码类型字段之前。

对于兼容html 5.3的较新浏览器,自动完成属性值“新密码”应该有效。

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

这两种方法的组合可用于支持较旧和较新的浏览器。

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

其他回答

这就是我们所说的文本框自动完成。

我们可以通过两种方式禁用文本框的自动完成:

按浏览器标签按代码要在浏览器中禁用,请转到设置

转到“高级设置”,取消选中复选框,然后单击“还原”。

如果要禁用编码标签,可以执行以下操作-

使用AutoCompleteType=“禁用”:

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

通过设置表单自动完成=“off”:

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

通过设置表单自动完成=“off”:

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

通过使用.cs页面中的代码:

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

通过使用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>

我尝试了几乎所有的答案,但新版本的Chrome很聪明;如果你写

autocomplete="randomstring" or autocomplete="rutjfkde"

它会自动将其转换为

autocomplete="off"

当输入控件接收到焦点时。

所以,我使用jQuery完成了这项工作,我的解决方案如下。

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

这是最简单的,对于表单上的任何数量的控件都可以做到。

在相关的或实际上,在完全相反的注释上-

“如果您是上述表单的用户,并希望重新启用自动完成功能,使用“记住密码”来自此bookmarklets的bookmarklet页它会删除页面上所有表单的所有autocomplete=“off”属性。保持好好战斗!"

Chrome的解决方案是在输入类型密码中添加autocomplete=“new password”。请检查以下示例。

例子:

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

如果Chrome找到一个密码类型的框,它总是自动完成数据,这足以表明该框的自动完成=“新密码”。

这对我很有用。

注意:使用F12确保更改生效。很多时候,浏览器都会将页面保存在缓存中,这给我一个不好的印象,认为它不起作用,但浏览器实际上并没有带来更改。

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