Checkbox Stays Checked on Page Refresh

不想你离开。 提交于 2019-12-18 05:48:10

问题


This a pretty simple question (I assume). Probably a repost, but I couldn't find the answer here... so here we go.

I have a checkbox on a page that I want to default to "unchecked" (unless I specify otherwise in my php).

When I refresh the page, if the box was checked, it will stay checked which is no-bueno because checking the box adds a dom element to my page via a function attached to the box. So the problem is if I refresh, the box is still checked, but the dom element doesn't exist (because I haven't fired the function and I don't want to unless the user checks the check box) but the box is ALREADY checked and I end up in opposite land where UN checking the box creates my dom element and checking it removes it.

Basically, the question is...

Is there a way to default a checkbox to unchecked without javascript?

BTW I haven't checked (no pun intended) in any browsers other than FF 3.5.10


回答1:


See http://weblogs.mozillazine.org/gerv/archives/2006/10/firefox_reload_behaviour.html (first comment):

It’s done that way on purpose so if you tap the refresh button you don’t lose your work. There’s an entry in Bugzilla somewhere that’s been WONTFIXed asking for a regular refresh to always reset the form entirely. Basically it’s a backwards-compatibility thing — every browser since NS1.0 (maybe even Mosiac) has done that.

Dynamically-generated pages don’t even reset themselves, though if the expiration is set to 0 and you hit the back button it will give you a fresh form. Also, if the form itself changes (add or remove elements, change the action, etc.) the for will reset on a reload. I haven’t tested it, but setting the form name to something random (assuming you don’t need the name for JS access) might just work. Like ”> in PHP.

As you said, forcing a refresh clears the form, and resetting it does too. Would something like do what you want (again, not tested)?




回答2:


this works for ie autocomplete="off"




回答3:


Sure:

document.getElementById('my_checkbox').checked = false;

Oops, I missed the part about no JS. As far as I know, Firefox retains page state on refresh, so I guess there's no other way.



来源:https://stackoverflow.com/questions/3176155/checkbox-stays-checked-on-page-refresh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!