Show ImageEven though the HTML stays sent to the browser is the same for each reload of the pa" />
Вопрос:

Why does the checkbox stay checked when reloading the page?

firefox

51955 просмотра

9 ответа

140496 Репутация автора

I'm reloading a web page that has the following code:

<label for="showimage">Show Image</label>
<input id="showimage" name="showimage" type="checkbox" value="1" />

Even though the HTML stays sent to the browser is the same for each reload of the page, the checkbox always takes on the checked value when a reload was performed. In other words, if the user checks the checkbox and reloads, the checkbox is still checked.

Is there some caching going on here?

Edit: I tried Gordon Bell's solution below and find that this is still happening even after removing the value="1". Anything else I might be missing?

<label for="showimage">Show Image</label> 
<input id="showimage" name="showimage" type="checkbox" /> 
Автор: Readonly Источник Размещён: 18.11.2008 07:16

Ответы (9)


35 плюса

68724 Репутация автора

Решение

Yes, I believe it is caching. I see this behaviour on Firefox for example (not Safari, for what that's worth :) ).

you can reload the page and bypass the cache (on Firefox) using CTRL-SHIFT-R and you'll see the check value doesn't carry (a normal CTRL-R will grab the info from the cache however)

edit: I was able to disable this server side on Firefox, setting a cache control header:

Cache-Control: no-store

this seems to disable the "remember form values" feature of Firefox

Автор: Owen Размещён: 18.11.2008 07:28

1 плюс

36344 Репутация автора

It is a nice feature of Firefox: if you type something but reload the page, the text remains in the text area. Idem for other settings you have chosen.

Alas, it doesn't work in SO (probably reset by JS) and dumber browsers like IE...

Which suggest a solution: if you really need to do that, reset the form with JS. form.reset() might do the job (acts like the Reset input button).

Автор: PhiLho Размещён: 18.11.2008 09:04

0 плюса

12134 Репутация автора

or instead of f5 press enter on address bar :)

Автор: Ionuț Staicu Размещён: 19.11.2008 12:47

0 плюса

951 Репутация автора

It could be due to a browser caching - very useful for static web sites that are not changed too often, very bad for dynamic web applications.
Try with those two meta tags in the head section of the page. Second meta tag is for older browsers (IE5) that are not recognizing "no-cache" meta tag and although different produces the same result: Each request goes to the server.

<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="-1">
Автор: Aleksandar Размещён: 19.11.2008 01:54

144 плюса

0 Репутация автора

Add autocomplete="off" into the form element on the page. The downside is that this isn't valid XHTML, but it fixes the issue without any convoluted javascript.

Автор: ironbirdman Размещён: 22.01.2009 10:39

30 плюса

446 Репутация автора

set autocomplete="off" with js is also working well.

for example using jquery:

$(":checkbox").attr("autocomplete", "off");
Автор: RainChen Размещён: 14.07.2009 04:03

0 плюса

24 Репутация автора

$("#showimage").prop("checked",false);

Автор: Sumit Mehta Размещён: 15.05.2017 07:25

0 плюса

11 Репутация автора

the public idea to solve that

make form & reset button

<form>
<checkbox>
<reset>
</form>

$(reset).trigger("click");//to clear the cache and input 
$(checkbox).trigger("click");//to mark checkbox
Автор: Mohamed Gomah Размещён: 06.09.2017 08:59

0 плюса

3 Репутация автора

This is an old question but still an active issue for firefox. None of the responses i tried solved it, but what did solve it for me was simply this:

    document.getElementById('formId').reset();

This simply resets the form to the default options every time the page loads. Not ideal since you lose granular control, but its the only thing that solved this for me.

Автор: cmshnrblu Размещён: 25.06.2019 06:12
Вопросы из категории :
32x32