blob: 0554028f27640d4feb4b3b2f07e9b385832fa8b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
---
title: <input type="checkbox">
slug: Web/HTML/Element/Input/checkbox
translation_of: Web/HTML/Element/input/checkbox
---
<p><span class="seoSummary">HTML input 元素 <strong><code><input type="checkbox"></code></strong> 允许你为表单中(或不在表单中)的提交项选择一个单一值。</span></p>
<div class="note">
<p><strong>注意</strong>: 若表单提交时,checkbox 未勾选,则提交的值并非为 value=unchecked;此时的值不会被提交到服务器。</p>
</div>
<h2 id="属性">属性</h2>
<p>该元素包含<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes">全局属性</a>。</p>
<dl>
<dt>{{htmlattrdef("checked")}}</dt>
<dd>
<p>当<strong>type</strong>属性的值为<code>checkbox 时</code>, 这个属性的存在表示 input 是否默认被选中。你可以将该属性设置为<code>checked="checked",</code>或更简单地只设置为<code>checked。</code></p>
</dd>
<dt>{{htmlattrdef("value")}}</dt>
<dd>指定 input 的值。如果省略,获取该元素的值的结果为字符串"on"。<br>
请注意,如果 value 值发生改变后页面重新加载,Gecko 和 IE<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=46845#c186">会忽略 HTML 源代码中设置的 value 值</a>。</dd>
<dt>{{htmlattrdef("indeterminate")}}</dt>
<dd>指定 checkbox 处于不定状态(在大多数平台,这样会产生一个划过 checkbox 的横线)。</dd>
</dl>
<h2 id="Example">示例</h2>
<pre class="brush: html"><p>
<input type="checkbox" id="cbox1" value="first_checkbox">
<label for="cbox1">This is the first checkbox</label>
</p>
<p>
<input type="checkbox" id="cbox2" value="second_checkbox" checked="checked">
<label for="cbox2">This is the second checkbox, which is checked</label>
</p>
</pre>
<p>以上代码将产生 2 个 checkbox,如下所示:</p>
<p>{{EmbedLiveSample("Example")}}</p>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<td>规范</td>
<td>状态</td>
</tr>
<tr>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', 'forms.html#checkbox-state-(type=checkbox)', '<checkbox>')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('HTML5 W3C', 'forms.html#checkbox-state-(type=checkbox)', '<checkbox>')}}</td>
<td>{{Spec2('HTML5 W3C')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('HTML4.01', 'interact/forms.html#checkbox', '<checkbox>')}}</td>
<td>{{Spec2('HTML4.01')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
{{Compat("html.elements.input.input-checkbox")}}
|