表單核心結構
<form action="/submit" method="post" novalidate>
<!-- 隱藏欄位:CSRF token 等 -->
<input type="hidden" name="csrf_token" value="abc123">
<div class="field">
<label for="email">電子郵件</label>
<input type="email" id="email" name="email" required autocomplete="email" placeholder="name@example.com">
</div>
<div class="field">
<label for="pwd">密碼</label>
<input type="password" id="pwd" name="password" required autocomplete="current-password" minlength="8">
</div>
<fieldset>
<legend>訂閱偏好</legend>
<label><input type="checkbox" name="subs[]" value="news"> 新聞快訊</label>
<label><input type="checkbox" name="subs[]" value="promo"> 優惠通知</label>
</fieldset>
<div class="field">
<label for="bio">自我介紹</label>
<textarea id="bio" name="bio" rows="4" maxlength="500" placeholder="簡單介紹自己...></textarea>
</div>
<div class="actions">
<button type="submit">送出</button>
<button type="reset">重置</button>
<button type="button" onclick="history.back()">返回</button>
</div>
</form>
input type 完整屬性對照表 (精選常用)
| type | 用途 | 關鍵屬性 | 驗證/行為 |
| text | 單行文字 | maxlength, minlength, pattern, placeholder, list, autocomplete, inputmode | 無自動驗證 |
| email | 電子郵件 | multiple (允許多個) | 格式驗證 |
| password | 密碼 (遮罩) | autocomplete="current-password|new-password" | 無內容驗證 |
| number | 數字 (含微調) | min, max, step | 數值範圍/步進 |
| date / datetime-local / month / week / time | 日期時間選擇器 | min, max, step | 格式 ISO 8601 |
| checkbox | 多選/單項確認 | checked, value, required (至少一) | 布林提交 |
| radio | 單選 (同 name 分組) | checked, value, required | 單一提交 |
| file | 檔案上傳 | accept, multiple, capture (camera/mic) | FormData 上傳 |
| submit / reset / button | 按鈕 | form*, type, value | 表單行為/無 |
| hidden | 隱藏資料 | value | 不顯示、隨表單提交 |
| color | 顏色選擇器 | value (HEX) | #rrggbb |
| range | 滑桿 | min, max, step | 數值提交 |
| search | 搜尋框 (含清除鈕) | placeholder, autosave, results | 同 text |
| tel / url | 電話/網址 | pattern, autocomplete | 格式驗證 (url 嚴格) |
共用屬性 (Global Attributes for Form Controls)
name:提交鍵名 (必填,除非 formnovalidate 或非表單按鈕)
value:初始/提交值
disabled:禁用 (不提交、不可聚焦)
readonly:唯讀 (可聚焦、可選取、會提交)
required:必填 (提交前驗證)
autocomplete:自動填入提示 (詳細 token 見 MDN)
autofocus:頁面載入自動聚焦 (每頁一個)
placeholder:提示文字 (非 <label> 替代)
form:關聯表單 id (HTML5 允許表單外控制)
aria-*:無障礙屬性 (aria-describedby, aria-invalid, aria-required)
data-*:自定義資料屬性 (JS 讀取 dataset)
無障礙表單模式
<div class="field">
<label for="username">使用者名稱</label>
<input type="text" id="username" name="username" required
aria-describedby="username-hint username-error"
autocomplete="username">
<span id="username-hint" class="hint">4-16 位英數字</span>
<span id="username-error" class="error" aria-live="polite" hidden></span>
</div>
<style>
.field { display: grid; gap: 0.25rem; margin-bottom: 1rem; }
label { font-weight: 500; }
input[aria-invalid="true"] { border-color: var(--color-error); }
.error:not([hidden]) { color: var(--color-error); font-size: 0.875rem; }
</style>
JavaScript 表單處理現代模式
const form = document.querySelector('form');
form.addEventListener('submit', async (e) => {
e.preventDefault();
const submitter = e.submitter; // 觸發提交的按鈕
if (!form.checkValidity()) {
form.reportValidity(); // 顯示瀏覽器原生錯誤 UI
return;
}
const data = new FormData(form, submitter); // 包含提交按鈕 name/value
try {
const res = await fetch(form.action, { method: form.method, body: data });
if (res.ok) { /* 成功導向或提示 */ }
} catch (err) { /* 網路錯誤處理 */ }
});
// 即時驗證回饋
form.querySelectorAll('input, select, textarea').forEach(el => {
el.addEventListener('blur', () => {
el.setAttribute('aria-invalid', !el.validity.valid);
el.nextElementSibling?.hidden = el.validity.valid;
});
});