添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

If you create a basic webpage with the following source:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script> </head> <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" /> </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.

Another interesting bit of information is that it still does the same thing if the input is a checkbox instead of a textbox .

Are you sure it's not just the Firefox "feature" where it remembers the state of input elements when you simply refresh? thirtydot May 12, 2011 at 23:51 @thirtydot: I was wondering about that too, so I also tried experimenting with dynamically setting the "size" attribute, and that does get reset upon refresh, just like all the other browsers. So it looks like what I've discovered so far is that Firefox will retain the disabled attribute as well as the actual value of the input, but not the size... Stephen Mesa May 12, 2011 at 23:56 Wow, you're right! I set autocomplete="off" on the input and this no longer happens. That's pretty inconvenient that firefox turns that on by default! Stephen Mesa May 12, 2011 at 23:58 Yeah, I'd forgotten you can disable it with autocomplete="off" . This blog post looks familiar to me, so I've definitely come across this before. You should write an answer to your own question (or should I?) thirtydot May 13, 2011 at 0:06 There is an open Mozilla bug report about this: bugzilla.mozilla.org/show_bug.cgi?id=654072 cvrebert Sep 3, 2014 at 23:57

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.

I just hit this problem when the user clicks the back button, seems like autocomplete="off" doesn't work in that case. solarc Sep 2, 2012 at 4:54 i don't want a form, I just have a single button and Firefox "remembers" it's disabled..annoying. I can reset it via JS but..ugly. vsync May 7, 2013 at 21:43 You even have to do this on buttons. I find it hard to believe that this is a feature and not a bug? Liam Mar 26, 2014 at 10:07

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);
                I don't know why this answer has only one up vote while the other answer has 99. Restoring the disabled state on unload is better than disabling autocomplete, since autocomplete is a desirable functionality.
– Nick
                Feb 13, 2017 at 4:04
                I think that //enable button here is redundant here; my comprehension of referred docs is that simply the presence of event listener will prevent the page to be stored in the BFcache.
– myf
                Jun 12, 2018 at 9:50
                Note that adding an unload handler has other side effects in Firefox: developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/1.5/…
– robocat
                Dec 7, 2018 at 1:12

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

  • comment on the bug report by a Bootstrap team member
  • bug report in the Bootstrap repository
  • note in the Bootstrap documentation
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.