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


当前回答

我真不敢相信,这件事被报道了这么久,仍然是一个问题。以前的解决方案对我来说不起作用,因为Safari似乎知道元素何时不显示或不在屏幕上,但下面的解决方案确实对我起作用:

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

其他回答

提供的答案在我测试的所有浏览器上都不起作用。基于已经提供的答案,这是我最终在Chrome 61、Microsoft Edge 40(EdgeHTML 15)、IE 11、Firefox 57、Opera 49和Safari 5.1上测试的结果。由于多次试验,它很古怪;然而,它确实对我有用。

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

只需将值为“off”的属性autocomplete设置为输入类型。

<input type="password" autocomplete="off" name="password" id="password" />

我真不敢相信,这件事被报道了这么久,仍然是一个问题。以前的解决方案对我来说不起作用,因为Safari似乎知道元素何时不显示或不在屏幕上,但下面的解决方案确实对我起作用:

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

定义输入的属性type=“text”和autocomplete=“off”足以为我关闭自动完成。

除了type=“password”之外,我尝试使用JavaScript钩住别人的建议中的onfocus/onfocuout事件来切换只读属性,效果很好,但当密码字段编辑开始为空时,自动完成密码再次出现。

我建议除了上述工作正常的解决方法之外,还可以使用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)" />

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

可以说,我想要一种可以将现场管理完全从浏览器手中拿走的东西。在本例中,有一个标准的文本输入字段来捕获密码-没有电子邮件、用户名等。。。

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

有一个名为“input”的变量,设置为空字符串。。。

var input = "";

jQuery监视字段事件。。。

聚焦时,始终清除字段内容和关联的“输入”变量。在按键时,任何字母数字字符以及一些定义的符号都会附加到“input”变量,并且字段输入会替换为项目符号。此外,当按下Enter键时,键入的字符(存储在“input”变量中)将通过Ajax发送到服务器。(请参阅下面的“服务器详细信息”。)在设置键时,Home、End和Arrow键会刷新“input”变量和字段值。(我可能会对箭头导航和焦点事件产生兴趣,并使用.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);
    }
});

前端摘要

本质上,这使浏览器无法捕获任何有用的内容。即使它覆盖了自动完成设置,和/或显示了一个包含先前输入值的下拉列表,它也只存储字段值的项目符号。


服务器详细信息(可选阅读)

如上所示,一旦服务器返回JSON响应,JavaScript就会执行location.reload()。(此登录技术用于访问受限制的管理工具。与cookie内容相关的一些过度使用可以被跳过,以实现更广泛的实现。)以下是详细信息:

当用户导航到站点时,服务器会查找合法的cookie。如果没有任何cookie,将显示登录页面。当用户输入密码并通过Ajax发送,服务器确认密码并检查以查看用户的IP地址是否在授权IP地址列表中。如果无法识别密码或IP地址,则服务器不会生成cookie,因此当页面重新加载时,用户会看到相同的登录页面。如果密码和IP地址都被识别,服务器将生成cookie具有十分钟的使用寿命,它还存储与时间帧和IP地址相对应的两个加扰值。当页面重新加载时,服务器会找到cookie并验证加扰值是正确的(即时间帧与cookie的日期相对应并且IP地址相同)。每当用户与服务器交互时,无论他们是登录、显示数据还是更新记录,都会重复验证和更新cookie的过程。如果cookie的值始终正确,服务器将显示完整的网站(如果用户正在登录)或满足提交的任何显示或更新请求。如果在任何时候cookie的值不正确,服务器会删除当前cookie,然后在重新加载时,会导致登录页面重新显示。