当刷新时输入未重置的 Firefox 禁用属性错误

我发现了一个我认为是 Firefox 的 bug,我想知道这是否真的是一个 bug,以及解决这个问题的方法。

如果你创建了一个基本的网页,其来源如下:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
</head>
<body>
<div>
<input id="txtTest" type="text" />
<input type="button" onclick="$('#txtTest').attr('disabled','disabled');" value="Set Disabled (jQuery)" />
<input type="button" onclick="document.getElementById('txtTest').disabled = true;" value="Set Disabled (js)" />
<input type="button" onclick="$('#txtTest').removeAttr('disabled');" value="Remove Disabled" />
</div>
</body>
</html>

如果您 disabletextbox动态,然后刷新页面,textbox将保持禁用,而不是重新设置回其未禁用的原始状态。我已经在 IE8和 Chrome 中尝试过这种方法,它们的表现和我预期的一样,刷新时将 textbox重新设置为不禁用。

另一个有趣的信息是,如果输入是 checkbox而不是 textbox,它仍然执行相同的操作。

50595 次浏览

This is a "feature" of Firefox which remembers form input values across page refreshes. To fix this behavior, you simply set autocomplete="off" on the form containing the inputs, or just directly to the input.

This stops autocomplete from working and prevents the browser from remembering the state of input fields.

Alternatively, you can just "hard-refresh" by clicking CTRL+F5. This will completely reset the current page.

To deal with the back button, do this (from here)

    window.addEventListener('pageshow', PageShowHandler, false);
window.addEventListener('unload', UnloadHandler, false);


function PageShowHandler() {
window.addEventListener('unload', UnloadHandler, false);
}


function UnloadHandler() {
//enable button here
window.removeEventListener('unload', UnloadHandler, false);
}

As mentioned before you need to add autocomplete="off" to your buttons.

Here is a sh+perl snippet to automate this in the case of <button>s in your HTML files/templates (under some assumptions):

find /path/to/html/templates -type f -name '*.html' -exec perl -pi -e \
's/(?<=<button )(.*?)(?=>)/@{[(index($1,"autocomplete=")!=-1?"$1":"$1 autocomplete=\"off\"")]}/g' \
{} +

The assumptions are:

  • Opening <button> tags begin and end on the same line. If this is not the case (i.e. they might be split over several lines) then replacing /g with /gs should help (the s modifier causes . to match newlines as well)

  • Valid HTML (e.g. there are no funny characters between < and >) and no unescaped greater than (>) inside the opening tag.

This is indeed an open bug in Firefox. There is also a note in MDN: autocomplete (scroll down to the second yellow box):

Note: The autocomplete attribute also controls whether Firefox will — unlike other browsers — persist the dynamic disabled state and (if applicable) dynamic checkedness of an <input> element, <textarea> element, or entire <form> across page loads. The persistence feature is enabled by default. Setting the value of the ABC0 attribute to off disables this feature. This works even when the autocomplete attribute would normally not apply by virtue of its type. See bug 654072.

If you are using Bootstrap, you might be interested in the