表單
單選按鈕
可從多個選項中選出一項的小圓形控制項,直接使用原生單選按鈕。
本頁內容
單選按鈕是小圓形控制項,選取後會填色並顯示圓點。圓形代表「只能選一項」,而方形的核取方塊代表「可選多項」。將 .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 的單選按鈕會形成一組。選取其中一項會取消其他選項,方向鍵可在群組內切換選取項目。請將群組放進含有 legend 元素的 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" 輸入控制項。 |