HTML:复选框默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12911787/
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
HTML: Checkbox default value
提问by Franz
When I submit a HTML form with a checked checkbox that doesn't have an explicitly defined value
, Chrome sends on
as a value for that field.
当我提交带有未明确定义的选中复选框的 HTML 表单时value
,Chrome 将on
作为该字段的值发送。
Is that the standard behavior? Or what can I expect from other browsers?
这是标准行为吗?或者我对其他浏览器有什么期望?
回答by Jukka K. Korpela
The HTML 4.01 specification does not specify the value of a checked checkbox. It just refers it saying that it is “on”, but this is just prose and does not say what the default value is. But it also says (under the description of the input
element) that the value
attribute is required in this case.
HTML 4.01 规范没有指定选中复选框的值。它只是指它说它是“打开”,但这只是散文,并没有说明默认值是什么。但它也说(在元素的描述下input
)value
在这种情况下需要该属性。
So <input type=checkbox name=foo>
has undefined behavior as regards to the value used, though in practice browsers use value=on
as the default.
<input type=checkbox name=foo>
关于所用值的未定义行为也是如此,尽管在实践中浏览器将其value=on
用作默认值。
回答by hakre
HTML Living Standardreflects this: The value is in mode "default/on", that means it's value is "on
" if no value attribute is provided. From 4.10.7.1.16 Checkbox state (type=checkbox) - HTML
Living Standard (Sep 2013):
HTML Living Standard反映了这一点:该值处于“default/on”模式,这意味着on
如果没有提供 value 属性,则它的值为“ ”。从4.10.7.1.16 复选框状态 (type=checkbox) - HTML Living Standard(2013 年 9 月)开始:
- The value IDL attribute is in mode default/on.
[...]
default/on
On getting, if the element has avalue
attribute, it must return that attribute's value; otherwise, it must return the string "on
". On setting, it must set the element'svalue
attribute to the new value.
- 值 IDL 属性处于模式default/on。
[...]
default/on
获取时,如果元素具有value
属性,则必须返回该属性的值;否则,它必须返回字符串“on
”。在设置时,它必须将元素的value
属性设置为新值。
This is quite identically also part of another HTML specification, the W3C HTML 5 Aug 2013 Recommendation Specificationhas this as well:
这也是另一个 HTML 规范的一部分,W3C HTML 5 Aug 2013 Recommendation Specification也有这个:
- http://www.w3.org/TR/html5/forms.html#checkbox-state-(type=checkbox)
- http://www.w3.org/TR/html5/forms.html#dom-input-value-default-on
- http://www.w3.org/TR/html5/forms.html#checkbox-state-(type=checkbox)
- http://www.w3.org/TR/html5/forms.html#dom-input-value-default-on
For reference my earlier comment:
供参考我之前的评论:
Firefox (Sep 2013), Chrome (Sep 2013), Internet Explorer (6): "on". I suspect this goes back a long way. http://lxr.mozilla.org/classic/source/lib/layout/layform.c#86- as most browsers need to have some default value for their own code objects I guess this "on" is just common.
Firefox(2013 年 9 月)、Chrome(2013 年 9 月)、Internet Explorer(6):“开启”。我怀疑这可以追溯到很久以前。http://lxr.mozilla.org/classic/source/lib/layout/layform.c#86- 因为大多数浏览器需要为自己的代码对象设置一些默认值,我猜这个“on”很常见。
回答by techfoobar
Browsers will send the value of a checkbox (in the POST data) only if it is checked. A check to see if a value (anyvalue) for a particular checkbox is present in the POST data is all you need.
浏览器将发送复选框的值(在 POST 数据中)只有在它被选中时。您只需要检查 POST 数据中是否存在特定复选框的值(任何值)即可。
i.e.
IE
// no need to check against 'on', 'true', '1' etc..
if(post data contains a value for checkbox1) {
// checkbox 1 is checked
}
else {
// not checked
}