Html Firefox 的错误 - 刷新时不重置输入的禁用属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5985839/
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
Bug With Firefox - Disabled Attribute of Input Not Resetting When Refreshing
提问by Stephen Mesa
I've found what I believe to be a bug with Firefox and I'm wondering if this actually is a bug, as well as any workarounds for this.
我发现我认为是 Firefox 的一个错误,我想知道这是否真的是一个错误,以及对此的任何解决方法。
If you create a basic webpage with the following source:
如果您使用以下来源创建基本网页:
<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>
If you disable
the textbox
dynamically and then refresh the page, the textbox
will remain disabled instead of resetting back to its original state of not disabled. I've tried this in IE8 and Chrome and those behave as I would expect, resetting the textbox
back to not disabled when I refresh.
如果您disable
的textbox
动态再刷新页面时,textbox
将保持禁用,而不是重新回到原来的未禁用的状态。我已经在 IE8 和 Chrome 中尝试过这个,它们的行为符合我的预期,textbox
刷新时将背面重置为未禁用。
Another interesting bit of information is that it still does the same thing if the input is a checkbox
instead of a textbox
.
另一个有趣的信息是,如果输入是 acheckbox
而不是 a ,它仍然会做同样的事情textbox
。
回答by Stephen Mesa
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.
这是Firefox 的一个“功能”,它可以在页面刷新时记住表单输入值。要修复此行为,您只需autocomplete="off"
在包含输入的表单上进行设置,或者直接设置为输入。
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.
或者,您可以通过单击 CTRL+F5 来“硬刷新”。这将完全重置当前页面。
回答by Joshua Fox
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);
}
回答by phk
As mentioned before you need to add autocomplete="off"
to your buttons.
如前所述,您需要添加autocomplete="off"
到按钮中。
Here is a sh
+perl
snippet to automate this in the case of <button>
s in your HTML files/templates (under some assumptions):
这是一个sh
+perl
片段,用于<button>
在 HTML 文件/模板中的s的情况下自动执行此操作(在某些假设下):
find /path/to/html/templates -type f -name '*.html' -exec perl -pi -e \
's/(?<=<button )(.*?)(?=>)/@{[(index(,"autocomplete=")!=-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 (thes
modifiercauses.
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.
开始
<button>
标记在同一行开始和结束。如果不是这种情况(即它们可能被分成几行),那么替换/g
为/gs
应该会有所帮助(s
修饰符.
也会导致匹配换行符)有效的 HTML(例如,
<
和之间没有有趣的字符>
)并且>
开始标签内没有大于 ( ) 的未转义字符。
回答by str
This is indeed an open bugin Firefox. There is also a note in MDN: autocomplete
(scroll down to the second yellow box):
这确实是Firefox 中的一个开放错误。MDN中autocomplete
还有一个注释:(向下滚动到第二个黄色框):
Note: The
autocomplete
attribute also controls whether Firefox will — unlike other browsers — persist the dynamic disabled state and (if applicable) dynamic checkednessof an<input>
element,<textarea>
element, or entire<form>
across page loads. The persistence feature is enabled by default. Setting the value of theautocomplete
attribute tooff
disables this feature. This works even when the autocomplete attribute would normally not apply by virtue of its type. See bug 654072.
注:该
autocomplete
属性也控制是否Firefox会-不像其他的浏览器-坚持动态禁用状态及(如适用)动态checkedness一个的<input>
元素,<textarea>
元素,或整个<form>
跨页的负荷。默认情况下启用持久性功能。将autocomplete
属性值设置为off
禁用此功能。即使自动完成属性由于其类型而通常不适用,这也有效。请参阅错误 654072。
If you are using Bootstrap, you might be interested in the
如果您正在使用 Bootstrap,您可能对