复选框在页面刷新时保持选中状态 [英] Checkbox Stays Checked on Page Refresh

查看:29
本文介绍了复选框在页面刷新时保持选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在页面上有一个复选框,我想将其默认为未选中"(除非我在 php 中另行指定).

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

当我刷新页面时,如果该框被选中,它将保持选中状态,这不是 bueno,因为选中该框会通过附加到框的函数向我的页面添加一个 dom 元素.所以问题是,如果我刷新,该框仍然被选中,但 dom 元素不存在(因为我还没有触发该函数,除非用户选中该复选框,否则我不想触发)但该框是ALREADY 选中,我最终在对面的地方 UN 选中该框会创建我的 dom 元素并选中它会删除它.

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.

基本上,问题是...

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

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

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

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

推荐答案

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

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

它是故意这样做的,所以如果你点击刷新按钮不要丢失您的工作.在 Bugzilla 的某个地方有一个条目被 WONTFIXed 要求定期刷新以始终重置表单完全.基本上这是一个向后兼容的东西——每个自 NS1.0(甚至可能是 Mosiac)以来的浏览器已经做到了.

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.

动态生成的页面甚至不会自行重置,但如果过期时间设置为 0 并且你点击后退按钮它会给你一个新鲜的形式.此外,如果表单本身发生变化(添加或删除元素,更改操作等) fo​​r 将在重新加载时重置.我没有对其进行了测试,但将表单名称设置为随机名称(假设您不需要 JS 访问的名称)可能会起作用.就像 PHP 中的>.

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

这篇关于复选框在页面刷新时保持选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆