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


当前回答

<!-- html -->
<input id="inputID" onfocus="this.readOnly = false;" ontouchstart="this.readOnly = false;" onblur="this.readOnly = true;" />

More

javascript和jQuery版本

其他回答

这就是:

函数turnOnPasswordStyle(){$('#inputpassword').attr('type',“password”);}<input-onput=“turnOnPasswordStyle()”id=“inputpassword”type=“text”>

大多数答案都没有帮助,因为浏览器只是忽略了它们。(其中一些不兼容跨浏览器)。对我有效的修复方法是:

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

我在表单标签上设置autofill=“off”,在不需要自动填充的地方设置autofil=“new password”。

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

这里提到的黑客在Chrome中没有一个对我有用。这里讨论了这个问题:https://code.google.com/p/chromium/issues/detail?id=468153#c41

将其添加到<form>中(至少目前如此):

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

如果不使用客户端和服务器端代码的组合,似乎不可能实现这一点。

为了确保用户每次都必须填写表单而不必自动完成,我使用了以下技术:

在服务器上生成表单字段名称,并使用隐藏的输入字段来存储这些名称,以便在提交到服务器时,服务器端代码可以使用生成的名称来访问字段值。这是为了阻止用户选择自动填充字段。在表单上放置每个表单字段的三个实例,并使用css隐藏每个集合的第一个和最后一个字段,然后在使用javascript加载页面后禁用它们。这是为了防止浏览器自动填写字段。

这里有一个fiddle演示了javascript、css和html,如#2所述https://jsfiddle.net/xnbxbpv4/

javascript代码:

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

css:

.disable-input {
  display: none;
}

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>

下面是一个使用asp.net和razor的服务器代码的粗略示例

型号:

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

控制器:

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);
    }
}

视图:

@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">
}