Html 如何明确设置复选框未选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5831105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to explicitly set checkbox unchecked
提问by Oleg Kubrakov
I have a xhtml page with transitional doctype having a checkbox that I want to be unchecked after loading. No JavaScript! In Firefox 3.5 (for instance, there may be other browsers) user can check input, than reload page and get input checked. How can I overcome this behaviour?
我有一个带有过渡文档类型的 xhtml 页面,其中有一个复选框,我想在加载后取消选中该复选框。没有 JavaScript!在 Firefox 3.5 中(例如,可能有其他浏览器)用户可以检查输入,而不是重新加载页面并检查输入。我怎样才能克服这种行为?
回答by TJ.
Use
用
autocomplete="off"
Also have a look at Why does the checkbox stay checked when reloading the page?
回答by alex
You can't do much to change a document's state with HTML alone. All you can do is set checked="checked"
or not.
仅使用 HTML 无法更改文档的状态。您所能做的就是设置checked="checked"
或不设置。
You need either JavaScript or a server side language to determine whether that attribute should be set or not.
您需要 JavaScript 或服务器端语言来确定是否应设置该属性。
回答by Pekka
You can't, not without JavaScript. This is Firefox specific behaviour which would occur even if you couldexplicitly force an "unchecked" state (which you can't, because the absence of checked
already means that.)
你不能,不能没有 JavaScript。这是 Firefox 特定的行为,即使您可以明确强制“未选中”状态(您不能,因为没有checked
已经意味着。)
The only non-Javascript way that I know of is to rename the form element on server-side on every request so FF has no chance of storing the value.
我所知道的唯一非 Javascript 方法是在每次请求时重命名服务器端的表单元素,这样 FF 就没有机会存储该值。
回答by Ionut
Try to put in your code this:
尝试在您的代码中输入:
<p><label><input id="test" type="checkbox"></input>Test checkbox</label></p>
It will be unchecked when the page starts or refreshes.
当页面启动或刷新时,它将被取消选中。