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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:24:25  来源:igfitidea点击:

HTML: Checkbox default value

htmlcross-browser

提问by Franz

When I submit a HTML form with a checked checkbox that doesn't have an explicitly defined value, Chrome sends onas 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 inputelement) that the valueattribute is required in this case.

HTML 4.01 规范没有指定选中复选框的值。它只是指它说它是“打开”,但这只是散文,并没有说明默认值是什么。但它也说(在元素描述下inputvalue在这种情况下需要该属性。

So <input type=checkbox name=foo>has undefined behavior as regards to the value used, though in practice browsers use value=onas 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 月)开始

[...]

default/on
On getting, if the element has a valueattribute, it must return that attribute's value; otherwise, it must return the string "on". On setting, it must set the element's valueattribute to the new value.

[...]

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也有这个:



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
}