表单
单选按钮
用小圆形凹槽表示选项,供用户从多项中选择一项,基于原生单选按钮。
本页内容
单选按钮是一个小圆形凹槽,选中时填入墨色并显示圆点。圆形表示只能选一项,复选框的方形则表示可以任选多项。将 .radio 加到原生 <input type="radio"> 上,再用 <label class="check"> 将它和标签文字包在一起,让整行都可点击。
单选按钮
<fieldset class="fieldset"> <legend class="fieldset-legend">Visibility</legend> <label class="check"> <input type="radio" class="radio" name="visibility" value="public" checked> Public </label> <label class="check"> <input type="radio" class="radio" name="visibility" value="team"> Team only </label> <label class="check"> <input type="radio" class="radio" name="visibility" value="private"> Private </label></fieldset><fieldset className="fieldset"> <legend className="fieldset-legend">Visibility</legend> <label className="check"> <input type="radio" className="radio" name="visibility" value="public" defaultChecked /> Public </label> <label className="check"> <input type="radio" className="radio" name="visibility" value="team" /> Team only </label> <label className="check"> <input type="radio" className="radio" name="visibility" value="private" /> Private </label></fieldset><fieldset class="fieldset"> <legend class="fieldset-legend">Visibility</legend> <label class="check"> <input type="radio" class="radio" name="visibility" value="public" checked> Public </label> <label class="check"> <input type="radio" class="radio" name="visibility" value="team"> Team only </label> <label class="check"> <input type="radio" class="radio" name="visibility" value="private"> Private </label></fieldset><template> <fieldset class="fieldset"> <legend class="fieldset-legend">Visibility</legend> <label class="check"> <input type="radio" class="radio" name="visibility" value="public" checked> Public </label> <label class="check"> <input type="radio" class="radio" name="visibility" value="team"> Team only </label> <label class="check"> <input type="radio" class="radio" name="visibility" value="private"> Private </label> </fieldset></template>#单选组
具有相同 name 的单选按钮组成一组。选中一项会取消组内其他项的选中状态,方向键可在组内切换选项。将整组放入带有组标题的 fieldset 中,用组标题写明这些选项要回答的问题。
#横向排列
少量简短选项可以并排放置。让选项之间的间距大于圆形按钮与其标签之间的间距,以便看清标签对应哪个按钮。
横向排列
<fieldset class="fieldset"> <legend class="fieldset-legend">Billing period</legend> <div class="flex flex-wrap gap-lg"> <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label> <label class="check"><input type="radio" class="radio" name="period"> Yearly</label> </div></fieldset><fieldset className="fieldset"> <legend className="fieldset-legend">Billing period</legend> <div className="flex flex-wrap gap-lg"> <label className="check"> <input type="radio" className="radio" name="period" defaultChecked /> Monthly </label> <label className="check"><input type="radio" className="radio" name="period" /> Yearly</label> </div></fieldset><fieldset class="fieldset"> <legend class="fieldset-legend">Billing period</legend> <div class="flex flex-wrap gap-lg"> <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label> <label class="check"><input type="radio" class="radio" name="period"> Yearly</label> </div></fieldset><template> <fieldset class="fieldset"> <legend class="fieldset-legend">Billing period</legend> <div class="flex flex-wrap gap-lg"> <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label> <label class="check"><input type="radio" class="radio" name="period"> Yearly</label> </div> </fieldset></template>#禁用
禁用后,单选按钮会淡化,标签改用禁用状态的墨色。可以禁用单个选项,也可以禁用其 fieldset 来禁用整组。
禁用
<fieldset class="fieldset"> <legend class="fieldset-legend">Region</legend> <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label> <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label> <label class="check"> <input type="radio" class="radio" name="region" disabled> Virginia, at capacity </label></fieldset><fieldset className="fieldset"> <legend className="fieldset-legend">Region</legend> <label className="check"> <input type="radio" className="radio" name="region" defaultChecked /> Tokyo </label> <label className="check"><input type="radio" className="radio" name="region" /> Frankfurt</label> <label className="check"> <input type="radio" className="radio" name="region" disabled /> Virginia, at capacity </label></fieldset><fieldset class="fieldset"> <legend class="fieldset-legend">Region</legend> <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label> <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label> <label class="check"> <input type="radio" class="radio" name="region" disabled> Virginia, at capacity </label></fieldset><template> <fieldset class="fieldset"> <legend class="fieldset-legend">Region</legend> <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label> <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label> <label class="check"> <input type="radio" class="radio" name="region" disabled> Virginia, at capacity </label> </fieldset></template>#如何选择控件
单选按钮会同时显示所有选项,每项占一行。也可以用以下控件实现单选:
#无障碍
- 为组内所有单选按钮设置相同的
name。缺少这一设置时,各选项可以同时选中,也无法用方向键在它们之间切换。 - 每组在 Tab 导航顺序中只占一个焦点位置。按 Tab 会聚焦到已选中的按钮,未选中任何项时则聚焦到第一个按钮。随后可用方向键在组内切换。
- 有合适的默认选项时,初始就选中它。如果初始没有选中任何项,用户一旦选中某项,就无法恢复到全未选中的状态。
#类名
| 类名 | 说明 |
|---|---|
radio | 原生 type="radio" input,呈圆形凹槽 |