以下用九個由淺入深的實例,把 HTML 表單常用元件逐一走過一遍。
網站如何與使用者互動?答案是表單。表單能把瀏覽者輸入的資料送到伺服器端,交由後端程式處理。
<form method="傳送方式" action="伺服器檔案">
所有表單控件——文字框、文字域、按鈕、單選框、複選框等——都必須寫在 <form></form> 之間,否則使用者輸入的內容根本送不到伺服器。get 與 post 的差異屬於後端課題,有興趣可另行深入。
<form method="post" action="save.php">
<label for="username">使用者名稱:</label>
<input type="text" name="username" />
<label for="pass">密碼:</label>
<input type="password" name="pass" />
</form>
要讓使用者鍵入字母、數字,就用文字輸入框;把 type 換成 password,輸入的內容便會被遮蔽。
<form>
<input type="text/password" name="名稱" value="文字" />
</form>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>文字輸入框、密碼輸入框</title>
</head>
<body>
<form method="post" action="save.php">
賬戶:<input type="text" name="myName" /><br><br>
密碼:<input type="password" name="pass" />
</form>
</body>
</html>
瀏覽器呈現:

需要輸入大段文字時,改用文字域。
<textarea rows="行數" cols="列數">預設文字</textarea>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>文字域</title>
</head>
<body>
<form action="save.php" method="post">
<label>個人簡介:</label><br>
<textarea cols="40" rows="10">在這裡輸入內容...</textarea><br>
<input type="submit" value="確定" name="submit" />
<input type="reset" value="重置" name="reset" />
</form>
</body>
</html>
瀏覽器呈現:

設計調查表時,與其讓使用者打字,不如給他選的。兩者的差別很單純:單選框只能選一項,複選框可以多選甚至全選。
<input type="radio/checkbox" value="值" name="名稱" checked="checked" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>單選框、複選框</title>
</head>
<body>
<form action="save.php" method="post">
你喜歡旅遊嗎?請選擇:<br>
<input type="radio" name="radiolove" value="like" checked="checked">喜歡
<input type="radio" name="radiolove" value="dislike">不喜歡
<input type="radio" name="radiolove" value="unknown">無所謂
<br><br>
你喜歡哪些運動?<br>
<input type="checkbox" name="checkbox" value="Run" checked="checked">Run
<input type="checkbox" name="checkbox" value="Basketball">Basketball
<input type="checkbox" name="checkbox" value="FootBall">FootBall
<input type="checkbox" name="checkbox" value="Fat" checked="checked">Fat
</form>
</body>
</html>
瀏覽器呈現:

這份範例做出一組三選一的單選框,以及一組四選多的複選框。
下拉列表能有效節省版面,單選、多選都支援。
<option value="name" selected="selected">Run</option>
value 是提交給伺服器的值;加上 selected="selected" 的選項會成為預設選中項。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>下拉列表框</title>
</head>
<body>
<form action="save.php" method="post">
<label>愛好:</label>
<select>
<option value="看書">看書</option>
<option value="旅遊" selected="selected">旅遊</option>
<option value="運動">運動</option>
<option value="購物">購物</option>
</select>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
<br><br>
<label>留言給我們:</label><br>
<textarea cols="40" rows="5">在這裡輸入留言...</textarea><br>
<input type="submit" value="點擊確認提交留言" name="advise">
</form>
</body>
</html>
瀏覽器呈現:

在 <select> 上加上 multiple="multiple",就能改為多選模式。Windows 下按住 Ctrl 再點擊可選多項,Mac 則用 Command 加點擊。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>使用下拉列表框進行多選</title>
</head>
<body>
<form action="save.php" method="post">
<label>愛好:</label><br>
<select multiple="multiple">
<option value="看書">看書</option>
<option value="旅遊">旅遊</option>
<option value="運動">運動</option>
<option value="購物">購物</option>
</select>
<br><br>
<label>留言給我們:</label><br>
<textarea cols="40" rows="5">在這裡輸入留言...</textarea><br>
<input type="submit" value="點擊確認提交留言" name="advise">
</form>
</body>
</html>
瀏覽器呈現:

<input type="submit" value="提交">
只有 type 設為 submit 時,按鈕才具備提交能力;value 是鈕上顯示的文字。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>提交按鈕</title>
</head>
<body>
<form method="post" action="save.php">
<label for="myName">姓名:</label>
<input type="text" value=" " name="myName" />
<input type="submit" value="提交" name="submitBtn" />
</form>
</body>
</html>
瀏覽器呈現:

當使用者輸入錯誤、想把表單回復到初始狀態時,重置按鈕就能派上用場。
<input type="reset" value="重置">
type 設為 reset 才有重置效果,value 同樣是鈕面文字。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>重置按鈕</title>
</head>
<body>
<form action="save.php" method="post">
<label>愛好:</label>
<select>
<option value="看書">看書</option>
<option value="旅遊" selected="selected">旅遊</option>
<option value="運動">運動</option>
<option value="購物">購物</option>
</select>
<input type="submit" value="確定" />
<input type="reset" value="重置" />
</form>
</body>
</html>
瀏覽器呈現:

label 本身不會帶來任何視覺效果,它的價值在於提升可用性:點擊 label 內的文字,瀏覽器會自動把焦點轉給對應的表單控件,也就是順帶把它選中。對小螢幕與行動裝置來說,這能大幅擴大可點擊的範圍。
<label for="控件id名稱">
務必注意:label 的 for 值必須與相關控件的 id 值完全相同,否則關聯不會生效。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>form 中的 label 標籤</title>
</head>
<body>
<form>
<label for="male">男</label>
<input type="radio" name="gender" id="male" /><br />
<label for="female">女</label>
<input type="radio" name="gender" id="female" /><br />
<label for="email">輸入你的郵箱地址</label>
<input type="email" id="email" placeholder="Enter email"><br>
你對什麼運動感興趣:<br>
<label for="run">慢跑</label>
<input type="checkbox" name="sports" id="run"><br>
<label for="climb">登山</label>
<input type="checkbox" name="sports" id="climb"><br>
<label for="basketball">籃球</label>
<input type="checkbox" name="sports" id="basketball"><br>
</form>
</body>
</html>
瀏覽器呈現:

九個實例走完,表單的骨架就完整了:form 定範圍與傳送方式,input 家族負責各類輸入,textarea 收長文,select 省空間,label 補可用性。真正的難點不在語法,而在 name 與 value 的規劃——那才是後端能不能正確收下資料的關鍵。