萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 表單常用標籤速查:input、select、textarea、button、fieldset、label 完整屬性表

表單核心結構

<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;
  });
});
copyright © 萬盛學電腦網 all rights reserved