原標題「表格控件」易誤解。表單控制項用於**蒐集使用者輸入**,主要標籤有 ``、`
| 分類 | 標籤/類型 | 用途 | 關鍵屬性 |
|---|---|---|---|
| 文字輸入 | `` | 一般單行文字 | `maxlength`, `pattern`, `placeholder` |
| `` | 密碼(遮罩) | `autocomplete="current-password"` | |
| `` | Email 格式驗證 | `multiple` 允許多個 | |
| `` | 網址格式驗證 | 需含協定 | |
| `` | 電話號碼 | `inputmode="tel"`, `pattern` | |
| `` | 搜尋框 | 部分瀏覽器有清除鈕 | |
| 多行文字 | ` | 長文本、備註、程式碼 | `rows`, `cols`, `wrap`, `maxlength` |
| 選擇類 | ` | 單選下拉選單 | `multiple`, `size`, `required` |
| ` | 多選下拉/清單 | 需配合 Ctrl/Cmd 點選 | |
| ` | 自動完成建議 | ` | |
| 布林/選項 | `` | 獨立開關/多選 | `value`(提交值),`checked` |
| `` | 單選群組 | 同 `name` 為一組,必填 `value` | |
| `` | 顏色選取器 | 回傳 `#rrggbb` | |
| `` | 檔案上傳 | `accept`, `multiple`, `capture` | |
| 日期時間 | `` | 年月日 | `min`, `max`, `step` |
| `` | 時分 | `min`, `max`, `step` | |
| `` | 本地日期時間 | 無時區 | |
| `` / `week` | 年月 / 年週 | ||
| 數值/範圍 | `` | 數字微調 | `min`, `max`, `step` |
| `` | 滑桿 | `min`, `max`, `step` | |
| ` | 計量/進度顯示 | 非輸入,僅顯示 | |
| 按鈕類 | `` | 提交表單 | `formaction`, `formmethod` 可覆蓋 |
| `` | 重置表單 | 慎用 | |
| ` | 功能同 input 但可包含 HTML 內容 | **推薦取代 input 按鈕** | |
| 隱藏/特殊 | `` | 傳遞 Token、CSRF、狀態 | 使用者不見 |
| `` | 圖片提交按鈕 | 舊式,傳送點擊座標 |
<form action="/register" method="post" novalidate>
<fieldset>
<legend>基本資料</legend>
<div class="field">
<label for="name">姓名</label>
<input type="text" id="name" name="name" required autocomplete="name">
</div>
<div class="field">
<label for="email">Email</label>
<input type="email" id="email" name="email" required autocomplete="email">
</div>
</fieldset>
<fieldset>
<legend>技能(可複選)</legend>
<div class="checkbox-group">
<label><input type="checkbox" name="skills[]" value="js"> JavaScript</label>
<label><input type="checkbox" name="skills[]" value="py"> Python</label>
<label><input type="checkbox" name="skills[]" value="go"> Go</label>
</div>
</fieldset>
<fieldset>
<legend>經驗等級</legend>
<div class="radio-group">
<label><input type="radio" name="level" value="junior" required> 初階</label>
<label><input type="radio" name="level" value="senior"> 高階</label>
</div>
</fieldset>
<button type="submit">送出申請</button>
</form>
const form = document.querySelector('form');
form.addEventListener('invalid', e => {
e.preventDefault();
const input = e.target;
input.setCustomValidity('');
if (input.validity.valueMissing) input.setCustomValidity('此欄位必填');
else if (input.validity.typeMismatch) input.setCustomValidity('格式不正確');
else if (input.validity.patternMismatch) input.setCustomValidity('請依提示格式輸入');
input.reportValidity();
}, true);
熟悉所有表單控制項類型與屬性,配合語意化結構與無障礙屬性,即可打造原生、健壯、使用者友善的表單體驗。