Html 禁用 Firefox 的自动填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6487970/
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 09:16:48  来源:igfitidea点击:

Disable Firefox's Auto-fill

htmlfirefox

提问by jeef3

Is it possible to disable Firefox's auto-fillfeature without disabling auto-complete?

是否可以在不禁用自动完成的情况下禁用 Firefox 的自动填充功能?

I know I can do this:

我知道我可以这样做:

autocomplete="off"

But I don't want to disable auto-complete, just the auto-fill.

但我不想禁用自动完成,只是自动填充。

Firefox is populating some of our hidden fields which are meant to be empty

Firefox 正在填充我们一些本应为空的隐藏字段

This is mostly a problem when the user refreshes the page. The form fields are re-populated with values from pre-refresh. An example of this being a problem is old-school place-holder. Where we populate the field with a value, and remove it on submit. The value is re-populated on refresh and we don't know if it's the place-holder or use value.

当用户刷新页面时,这主要是一个问题。使用预刷新中的值重新填充表单字段。这个问题的一个例子是老式的占位符。我们用一个值填充该字段,并在提交时将其删除。该值在刷新时重新填充,我们不知道它是占位符还是使用值。

采纳答案by Mike

If the problem is that FF is populating the fields when the user refreshes, this is actually a feature of Firefox to try to help the user in the case of accidental refresh so they don't lose whatever they have typed. I don't think you can override this with the HTML. You could use JavaScript to clear/reset all the relevant form values on page load. If a form.reset() on the form doesn't work, you will have to iterate over the form elements and clear them like this:

如果问题是当用户刷新时 FF 正在填充字段,这实际上是 Firefox 的一个功能,它试图在意外刷新的情况下帮助用户,这样他们就不会丢失他们输入的任何内容。我认为你不能用 HTML 覆盖它。您可以使用 JavaScript 在页面加载时清除/重置所有相关的表单值。如果表单上的 form.reset() 不起作用,您将必须遍历表单元素并像这样清除它们:

for (i = 0; i < frm_elements.length; i++)
{
    field_type = frm_elements[i].type.toLowerCase();
    switch (field_type)
    {
    case "text":
    case "password":
    case "textarea":
    case "hidden":
        frm_elements[i].value = "";
        break;
    case "radio":
    case "checkbox":
        if (frm_elements[i].checked)
        {
            frm_elements[i].checked = false;
        }
        break;
    case "select-one":
    case "select-multi":
        frm_elements[i].selectedIndex = -1;
        break;
    default:
        break;
    }
}

回答by user3193832

Putting two dummy fields in between the actual username field and the actual password field worked for me, e.g.:

在实际用户名字段和实际密码字段之间放置两个虚拟字段对我有用,例如:

<input name = "Username" type="text" autocomplete="off">
<input name = "DummyUsername" type="text" style="display:none;">
<input name = "DummyPassword" type="password" style="display:none;">
<input name = "Password" type="password" autocomplete="new-password">

This prevented the autofill of the username, as well as the autofill of the password.

这阻止了用户名的自动填充以及密码的自动填充。

autocomplete="new-password" is apparently not fully implemented, but can help with some browsers. I doubt that it was helpful with FireFox, but it is there for future versions in case the feature is more widely adopted.

autocomplete="new-password" 显然没有完全实现,但可以帮助某些浏览器。我怀疑它对 FireFox 是否有帮助,但是如果该功能被更广泛地采用,它会在未来版本中使用。

回答by Edward Olamisan

I've had a similar issue for the password field in a form on the page.

对于页面上表单中的密码字段,我遇到了类似的问题。

If there is an input tag with type = password in a form tag on the page Firefox is just automatically populating the password:

如果页面上的表单标签中有 type = password 的输入标签,Firefox 会自动填充密码:

<form>
    <input type='password' />
</form>

Here's the code I used to fix this specific issue:

这是我用来解决此特定问题的代码:

$('input[type=password]').val('')

回答by Craig White

Firefox only honors autocomplete="off"on the form, not individual elements (unlike IE).

Firefox 只尊重autocomplete="off"表单,而不是单个元素(与 IE 不同)。

Another option is to change the field names to strange things like name="_u_sern_ame"to confuse any field recognition systems if you want to disable the autofill of someones details.

另一种选择是将字段名称更改为奇怪的名称,例如name="_u_sern_ame"如果您想禁用某人详细信息的自动填充功能,则会混淆任何字段识别系统。

回答by superwf

autocomplete='off'will not prevent whether in input or form put a

autocomplete='off'不会阻止是否在输入或表单中放置一个

<input type='password' style='display: none;' />

before your real password input.

在您输入真实密码之前。

回答by Sygmoral

I had this problem myself. Mike's answer is close, but not perfect in my opinion: it empties elements, even if a valueattribute may want to set it.

我自己也有这个问题。Mike 的回答很接近,但在我看来并不完美:它会清空元素,即使value属性可能想要设置它。

On pageload, I do the following. It uses only a little jQuery.

在页面加载时,我执行以下操作。它只使用了一点点 jQuery。

// Reset input elements to their HTML attributes (value, checked)
$("input").each(function(){
    // match checkbox and radiobox checked state with the attribute
    if((this.getAttribute('checked')==null) == this.checked)
        this.checked = !this.checked;
    // reset value for anything else
    else this.value = this.getAttribute('value')||'';
});

// Select the option that is set by the HTML attribute (selected)
$("select").each(function(){
    var opts = $("option",this), selected = 0;
    for(var i=0; i<opts.length; i++) 
        if(opts[i].getAttribute('selected')!==null) 
            selected = i;

    this.selectedIndex = selected||0;
}); 


No jQuery?


没有jQuery?

Use document.getElementsByTagNameto select the inputs and selects, then iterate over them and replace thisby the iterated element.
Select the options with .getElementsByTagName('option').

使用document.getElementsByTagName选择inputS和selectS,然后在它们之间迭代并更换this由迭代元素。
用 选择选项.getElementsByTagName('option')

回答by wimh

Thispost suggest to use (other) dummy fields which will be autofilled, you can hide those with css. But this would probably work better with password fields than others.

这篇文章建议使用将自动填充的(其他)虚拟字段,您可以使用 css 隐藏它们。但这可能比其他密码字段更适用于密码字段。

回答by colmadden

This actually works with a password field in FireFox:

这实际上适用于 FireFox 中的密码字段:

$(window).load(function(){
    $('#pass').val('');
});

回答by Grasper

what worked for me is

对我有用的是

autocomplete="new-password"

回答by Maxim

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 forcing the no-autocompletion is to assign a random string to the attribute, for example:

在某些情况下,即使自动完成属性设置为关闭,浏览器也会不断建议自动完成值。对于开发人员来说,这种意外行为可能令人费解。真正强制非自动完成的技巧是为属性分配一个随机字符串,例如:

autocomplete="nope"

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

Tried in major browsers and it works!

在主要浏览器中尝试过,它有效!