html - 复选框在页面刷新时保持选中状态

标签 html checkbox

这是一个非常简单的问题(我假设)。可能是转发,但我在这里找不到答案...所以我们开始吧。

我在页面上有一个复选框,我想将其默认为“未选中”(除非我在我的 php 中另外指定)。

当我刷新页面时,如果选中该框,它将保持选中状态,这是 no-bueno,因为选中该框会通过附加到该框的函数向我的页面添加一个 dom 元素。所以问题是,如果我刷新,该框仍处于选中状态,但 dom 元素不存在(因为我没有触发该功能,除非用户选中该复选框,否则我不想这样做)但是该框是ALREADY 选中,我最终到达了相反的地方,UN 选中该框创建了我的 dom 元素,选中它删除了它。

基本上,问题是...

有没有办法在没有 javascript 的情况下将复选框默认为未选中?

顺便说一句,除了 FF 3.5.10,我还没有在任何浏览器中检查过(没有双关语)

最佳答案

参见 http://weblogs.mozillazine.org/gerv/archives/2006/10/firefox_reload_behaviour.html (第一条评论):

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)?

关于html - 复选框在页面刷新时保持选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3176155/

相关文章:

javascript - 延迟长时间运行的计时器任务以提高滚动平滑度

HTML TextArea 适合内容(在页面加载时)

python - Django:编写一个 View 来删除带有复选框的项目

html - CSS & HTML :checked property

html - 如何让 flex 元素扩展到每行最多 4 个元素的可用宽度?

html - 如何在html css中将滚动条添加到父div?

javascript - 如何使用 jQuery 将两个 html 包含到另一个 html 中

android - 如何在Android Kotlin的RecyclerView中为CheckBox添加isChecked和unChecked事件

javascript - 将复选框限制为 2,但移动选择而不是阻止它

javascript - 当取消选择一项或多项时,knockoutjs 取消选择/选择所有复选框