<input> 是 HTML 表單中最基礎、變化最多的輸入元素。透過 type 屬性切換 20+ 種介面,配合豐富驗證屬性,構建現代 Web 表單基石。
| 分類 | type 值 | 用途 | 關鍵屬性 | UI 特徵 |
|---|---|---|---|---|
| 文字類 | text | 單行文字 | maxlength, pattern, placeholder | 單行輸入框 |
password | 密碼 (遮罩) | minlength, autocomplete="current-password" | 圓點/星號遮罩 | |
email | 電子郵件 | multiple (多信箱) | 內建格式驗證、鍵盤優化 | |
url | 網址 | pattern 自訂 | 內建格式驗證 | |
tel | 電話 | pattern 格式化 | 行動裝置數字鍵盤 | |
search | 搜尋框 | — | 部分瀏覽器有清除鈕 | |
| 選擇類 | checkbox | 核取方塊 (多選) | checked, value, indeterminate (JS) | 方框勾選 |
radio | 單選按鈕 | name 分組, checked, value | 圓形單選 | |
file | 檔案上傳 | accept, multiple, capture | 檔案選擇器 | |
color | 顏色選擇器 | — | 瀏覽器原生色盤 | |
| 日期時間類 | date | 日期 (年月日) | min, max, step | 日曆選擇器 |
datetime-local | 本地日期時間 | min, max, step | 日期+時間選擇 | |
month | 年月 | min, max | 年月選擇 | |
week | 年週數 | min, max | 週曆選擇 | |
time | 時間 (時分秒) | min, max, step | 時間選擇器 | |
| 數值類 | number | 數字 (含微調) | min, max, step | 數字輸入+上下鍵 |
range | 滑桿 | min, max, step | 視覺化滑桿 | |
hidden | 隱藏欄位 | value | 不顯示,隨表單提交 | |
| 按鈕類 | submit | 送出表單 | value (按鈕文字) | 送出按鈕 |
reset | 重置表單 | value (按鈕文字) | 重置按鈕 | |
button | 通用按鈕 | — | 需 JS 綁定 | |
| 特殊 | image | 圖片送出按鈕 | src, alt, width, height | 圖片按鈕,提交座標 |
name:**必填**。表單提交時的鍵名,未設定則不送出資料。value:初始值/提交值。checkbox/radio 必須指定 value,否則提交 "on"。required:必填驗證 (HTML5 內建,提交前攔截)。disabled:停用 (不可聚焦、不提交、樣式灰化)。readonly:唯讀 (可聚焦、可複製、會提交)。placeholder:提示文字 (灰色,聚焦消失),**非替代 label**。autocomplete:控制瀏覽器自動完成 (on, off, username, current-password, email...)。id:搭配 <label for="id"> 綁定,擴大點擊區域、無障礙必要。form:關聯表單 ID (HTML5),input 可在 form 外部。autofocus:頁面載入自動聚焦 (每頁僅一個)。inputmode:虛擬鍵盤模式 (text, numeric, decimal, email, url, search, tel)。enterkeyhint:Enter 鍵提示 (enter, done, go, next, previous, search, send)。pattern:正則表達式驗證 (如 pattern="[0-9]{4}" 4 位數字)。min/max:數值/日期範圍。step:遞增間隔 (預設 1,any 任意)。minlength/maxlength:文字長度限制。accept (file):MIME 類型或副檔名過濾 (accept="image/*,.pdf")。<form action="/api/register" method="post" novalidate>
<div class="field">
<label for="email">電子郵件</label>
<input type="email" id="email" name="email" required
placeholder="name@example.com"
autocomplete="email"
aria-describedby="emailHint">
<span id="emailHint" class="hint">我們不會分享您的信箱</span>
</div>
<div class="field">
<label for="pwd">密碼</label>
<input type="password" id="pwd" name="password" required
minlength="8"
autocomplete="new-password"
aria-describedby="pwdRule">
<span id="pwdRule" class="hint">至少 8 字元,含大小寫與數字</span>
</div>
<fieldset>
<legend>訂閱方案</legend>
<label><input type="radio" name="plan" value="free" checked> 免費版</label>
<label><input type="radio" name="plan" value="pro"> 專業版 $299/月</label>
<label><input type="radio" name="plan" value="enterprise"> 企業版</label>
</fieldset>
<div class="field">
<label for="avatar">大頭照</label>
<input type="file" id="avatar" name="avatar" accept="image/*" capture="user">
</div>
<button type="submit">建立帳號</button>
<button type="reset">重新填寫</button>
</form>
input:valid:not(:placeholder-shown) { border-color: #198754; }
input:invalid:not(:placeholder-shown) { border-color: #dc3545; }
input:focus:invalid { box-shadow: 0 0 0 0.25rem rgba(220, 53, 69, 0.25); }
input[required] + label::after { content: " *"; color: #dc3545; }
<label for="id"> (或包裹在 label 內)。aria-describedby 關聯 role="alert" 區塊。required、aria-required="true" 雙重保障。<fieldset> + <legend>。placeholder 當作標籤替代 (消失後無提示)。精通 <input> 類型與屬性,能大幅減少自訂 JS 驗證代碼,提升使用者體驗、無障礙與安全性。