萬盛學電腦網

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

XHTML 表單標籤入門:form、input 核心屬性與無障礙實踐

表單是 Web 互動核心,XHTML 嚴格規範標籤結構與屬性。本文涵蓋 <form> 容器、<input> 兩大基礎類型(文字/密碼)、無障礙屬性與後端銜接概念。

表單容器 <form>

<form action="/submit" method="post" id="login-form">
  <!-- 表單控件 -->
</form>
屬性說明常用值
action提交目標 URL相對/絕對路徑
methodHTTP 方法get (查詢)、post (異動)
enctype編碼類型application/x-www-form-urlencoded (預設)、multipart/form-data (檔案上傳)
id/name表單識別碼JS 選取/提交參考

輸入控件 <input>

文字方塊

<label for="username">姓名:</label>
<input type="text" id="username" name="user" placeholder="請輸入姓名" required />

密碼方塊

<label for="password">密碼:</label>
<input type="password" id="password" name="pass" autocomplete="current-password" />

關鍵無障礙與規範要點

  1. label 綁定:for 對應 id,點擊文字聚焦控件
  2. 空元素自閉合:<input /> 符合 XHTML
  3. required/placeholder/autocomplete:HTML5 增強屬性,提升體驗與安全
  4. 後端處理:XHTML 僅負責前端結構,資料驗證、儲存需 PHP/ASP/Node.js 等後端語言配合

常見擴充控件(簡介)

  • type="radio":單選群組(同 name)
  • type="checkbox":多選/布林
  • type="submit/reset/button":提交/重置/自定義按鈕
  • <select><option>:下拉選單
  • <textarea>:多行文字
  • <fieldset><legend>:分組語意化(詳見 ID 35018)
copyright © 萬盛學電腦網 all rights reserved