HTML复选框的checked属性的值是什么? [英] What's the proper value for a checked attribute of an HTML checkbox?

查看:557
本文介绍了HTML复选框的checked属性的值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道如何在HTML中形成复选框输入:

We all know how to form a checkbox input in HTML:

<input name="checkbox_name" id="checkbox_id" type="checkbox">

我不知道什么是复选框的技术上正确的值?我已经看到这些工作:

What I don't know -- what's the technically correct value for a checked checkbox? I've seen these all work:

<input name="checkbox_name" id="checkbox_id" type="checkbox" checked>
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="on">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="yes">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="checked">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="true">

答案是否没关系?我没有看到标记为正确的答案的证据此处来自规范本身:

Is the answer that it doesn't matter? I see no evidence for the answer marked as correct here from the spec itself:


复选框(和单选按钮)是开/关开关,用户可以切换
。当控制元素的选中
属性被设置时,开关是on。当表单提交时,只有on复选框
控件可以成功。表单中的几个复选框可以共享
相同的控件名称。因此,例如,复选框允许用户
为同一属性选择几个值。 INPUT元素用于
创建复选框控件。

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful. Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property. The INPUT element is used to create a checkbox control.

规范作者说的是正确的答案?请提供循证解答。

What would a spec writer say is the correct answer? Please provide evidence-based answers.

推荐答案

严格来说,您应该根据规范=http://www.w3.org/TR/html-markup/input.checkbox.html>此处,最正确的版本是:

Strictly speaking, you should put something that makes sense - according to the spec here, the most correct version is:

<input name="name" id="id" type="checkbox" checked="checked">

对于HTML,您还可以使用空属性语法 checked = c $ c>已选中(对于更严格的XHTML,这是不支持的)。

For HTML, you can also use the empty attribute syntax, checked="", or even simply checked (for stricter XHTML, this is not supported).

然而,有效地,大多数浏览器将只支持引号之间的任何值。将检查所有以下内容:

Effectively, however, most browsers will support just about any value between the quotes. All of the following will be checked:

<input name="name" id="id" type="checkbox" checked>
<input name="name" id="id" type="checkbox" checked="">
<input name="name" id="id" type="checkbox" checked="yes">
<input name="name" id="id" type="checkbox" checked="blue">
<input name="name" id="id" type="checkbox" checked="false">

只有以下选项才会取消选中:

And only the following will be unchecked:

<input name="name" id="id" type="checkbox">

另请参见 disabled =disabled

这篇关于HTML复选框的checked属性的值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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