<input> 核心定位<input> 是**空元素**(Void Element),**無結束標籤**,HTML5 建議自閉合 <input />。它是表單輸入的核心,**行為完全由 type 屬性決定**,涵蓋文字、選擇、按鈕、隱藏傳值等 20+ 類型。
| type | 用途 | UI 呈現 | 關鍵屬性 | 驗證/提交行為 |
|---|---|---|---|---|
text | 單行文字 | 文字框 | maxlength, pattern, placeholder, list | 無內建驗證 |
password | 密碼 | 彈幕字符 | autocomplete="current-password", minlength | 必須 HTTPS 傳輸 |
email | 郵件 | 文字框 + @鍵盤 | multiple 允許多信箱 | 瀏覽器內建格式驗證 |
tel | 電話 | 文字框 + 數字鍵盤 | pattern 自訂格式 | 無標準格式,需自訂 pattern |
url | 網址 | 文字框 + .com鍵盤 | — | 瀏覽器驗證 URL 格式 |
number | 數值 | 微調箭頭 | min, max, step | 僅允許數字,step="any" 允許小數 |
date | 日期 | 原生日期選擇器 | min, max, step | 格式 YYYY-MM-DD,無時區 |
datetime-local | 日期時間 | 原生日期時間選擇器 | min, max, step | 格式 YYYY-MM-DDTHH:MM,無時區 |
month | 年月 | 原生選擇器 | min, max | 格式 YYYY-MM |
week | 年週 | 原生選擇器 | min, max | 格式 YYYY-Www |
time | 時間 | 原生時間選擇器 | min, max, step | 格式 HH:MM[:ss] |
color | 顏色 | 顏色選擇器 | list 關聯 datalist | 提交 #RRGGBB 格式 |
checkbox | 多選/同意 | 方框 | checked, value (必填) | 未勾選**不提交** |
radio | 單選組 | 圓框 | 同 name 分組, value 必填, checked | 同組單選,必須有預設 |
file | 檔案上傳 | 選擇檔案按鈕 | accept, multiple, capture | accept="image/*" 限類型,伺服端必驗 |
range | 範圍滑桿 | 滑桿 | min, max, step | 提交數值字串 |
search | 搜尋框 | 文字框 (含清除鈕) | placeholder | 語意化 search,語音搜尋入口 |
submit | 提交按鈕 | 按鈕 | value 為按鈕文字, formaction 覆蓋 form action | 觸發表單提交 |
button | 普通按鈕 | 按鈕 | value 為按鈕文字 | **無預設行為**,需 JS 綁定 |
reset | 重置按鈕 | 按鈕 | — | 重置表單初始值,**慎用** |
hidden | 隱藏傳值 | 不顯示 | value 必填 | 用戶可見原始碼,**勿存敏感資訊** |
image | 圖片提交按鈕 | 圖片 | src, alt, width, height | 提交座標 x,y,舊式用法 |
name — 表單數據鍵名(必填)伺服端依 name 接收值。radio/checkbox 同名分組。id 用於 <label for> 關聯,**不可代替 name**。
value — 初始值/提交值checkbox/radio:**勾選時提交的值,必須指定**,否則預設 "on"。hidden:固定傳值(CSRF Token、記錄 ID)。button/submit/reset/image:按鈕顯示文字。autocomplete — 瀏覽器自動填入控制(關鍵 UX)<!-- 關閉 -->
<input type="password" name="pwd" autocomplete="off">
<-- 語意化標準值(推薦) -->
<input type="email" name="email" autocomplete="email">
<input type="text" name="name" autocomplete="name">
<input type="tel" name="tel" autocomplete="tel">
<input type="text" name="address" autocomplete="street-address">
<input type="text" name="city" autocomplete="address-level2">
<input type="text" name="postal" autocomplete="postal-code">
<input type="password" name="pwd" autocomplete="current-password">
<input type="password" name="new-pwd" autocomplete="new-password">
<input type="text" name="otp" autocomplete="one-time-code">
<input type="text" name="cc" autocomplete="cc-number">
<input type="text" name="cc-exp" autocomplete="cc-exp">
<input type="text" name="cc-csc" autocomplete="cc-csc">
完整清單見 HTML Standard Autofill。正確設定可提升 30%+ 表單完成率。
<input type="text" name="username" required minlength="3" maxlength="20" pattern="^[a-zA-Z0-9_]+$" title="英數底線 3-20 字">
<input type="number" name="age" min="18" max="120" step="1" required>
<input type="file" name="avatar" accept="image/*" required>
鐵律: 客戶端驗證**僅為 UX**,**伺服端必須重複驗證**。
id + <label for="id">:**強制關聯**,點擊標籤聚焦輸入框,螢幕閱讀器朗讀標籤文本。aria-describedby="hint-id":關聯補充說明/錯誤訊息元素。aria-invalid="true":JS 驗證失敗時動態添加,配合錯誤訊息 role="alert"。required、aria-required="true":雙重保險。<form action="/register" method="post" novalidate class="form">
<div class="form-group">
<label for="email">電子信箱</label>
<input type="email" id="email" name="email" required autocomplete="email" aria-describedby="email-hint">
<span id="email-hint" class="form-hint">註冊後將發送驗證信</span>
<span class="form-error" role="alert" aria-live="polite"></span>
</div>
<fieldset>
<legend>訂閱偏好</legend>
<div class="checkbox-group">
<label><input type="checkbox" name="subs[]" value="news"> 產品新聞</label>
<label><input type="checkbox" name="subs[]" value="offer"> 優惠通知</label>
</div>
</fieldset>
<button type="submit" class="btn btn-primary">建立帳號</button>
</form>
<input>、<select>、<date> 等原生 UI 受作業系統/瀏覽器強烈影響,跨平台一致性極差。
/* 統一基礎重置 */
input, select, textarea, button {
font: inherit; /* 繼承字體 */
color: inherit;
background: #fff;
border: 1px solid #ccc;
border-radius: 4px;
padding: 8px 12px;
box-sizing: border-box; /* 關鍵:padding 不撐大寬高 */
}
/* 移除原生外觀(WebKit/Blink/Firefox) */
input[type="search"] { -webkit-appearance: none; appearance: none; }
input[type="date"]::-webkit-calendar-picker-indicator { opacity: 0; } /* 隱藏原生圖標 */
select { appearance: none; background-image: url("data:image/svg+xml,..."); /* 自訂箭頭 */ }
這些庫處理了極其複雜的鍵盤導航、螢幕閱讀器語意、焦點管理、動畫、國際化,**不要重複造輪子**。
<input> 搭配 <datalist> 自動完成<input type="text" name="city" list="cities" placeholder="輸入城市">
<datalist id="cities">
<option value="台北市"></option>
<option value="新北市"></option>
<option value="桃園市"></option>
</datalist>
原生輕量自動完成,無需 JS 庫。
<input type="hidden" name="csrf_token">)。file 上傳:伺服端驗證 MIME、副檔名、掃毒、重命名存儲、非網頁目錄、獨立域名/CDN 提供。password 欄位:autocomplete="new-password" (註冊/改密)、autocomplete="current-password" (登入)。<input> 類型豐富、屬性精細、原生驗證強大、無障礙內建。善用 type、autocomplete、pattern、required、語意化 <label>/<fieldset>,配合現代組件庫處理樣式化難題,即可構建專業、安全、無障礙、高轉換率的 Web 表單。