HTML 复选框的选中属性的正确值是多少? [英] What's the proper value for a checked attribute of an HTML checkbox?

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

问题描述

我们都知道如何在 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:

复选框(和单选按钮)是可以切换的开/关开关由用户.当控制元素被选中时,开关处于打开"状态属性设置.提交表单时,只有开启"复选框控制可以成功.表单中的多个复选框可以共享相同的控件名称.因此,例如,复选框允许用户为同一属性选择多个值.使用 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.

推荐答案

严格来说,你应该放一些有意义的东西 - 根据规范 这里,最正确的版本是:

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="",甚至简单的 checked(对于更严格的 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" 上的类似问题.

See also this similar question on disabled="disabled".

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

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