在 HTML5 增強的眾多元素中,最值得投入注意力的一定是表單。表單在這次改版中幾乎被翻新了一遍,過去必須靠 JavaScript 手寫的一堆互動與驗證功能,現在單靠標籤屬性就能搞定。
開始之前,先記住一個關鍵觀念:
在 HTML5 中,表單控制項可以位於它所屬表單的外部。因此 fieldset、label、input 等元素都新增了 form 屬性,用來標示自己歸屬於哪一個表單。
除了新增 form 屬性外,label 還有個值得多說兩句的 for。for 用於指定該 label 對應的表單控制項,點擊 label 時,對應控制項會取得焦點:
<form action="demo_form.asp" id="form1">
<label for="name">Click Me</label><input id="name" type="text"></input>
<input type="submit" value="Submit" />
</form>
點一下「Click Me」,後面的輸入框就會獲得焦點。
除了原有的 button、text、submit、checkbox、radio、select、password,HTML5 加入了不少新類型,用於對資料進行組織與歸類。遺憾的是,當時沒有任何一款瀏覽器能完整支援全部類型。
可以用下面的範例實際測試各家瀏覽器的支援度:
<form action="demo_form.asp">
Select your favorite color: <input type="color" name="favcolor" />
Birthday: <input type="date" name="bday" />
Birthday (date and time): <input type="datetime" name="bdaytime" />
Birthday (local date and time): <input type="datetime-local" name="bdaytime" />
Birthday (month and year): <input type="month" name="bdaymonth" />
Select a time: <input type="time" name="usr_time" />
Select a week: <input type="week" name="week_year" />
Quantity (between 1 and 5): <input type="number" name="quantity" min="1" max="5" />
Points (between 1 and 10): <input type="range" name="points" min="1" max="10" />
Search Google: <input type="search" name="googlesearch" />
Telephone: <input type="tel" name="usrtel" />
Add your homepage: <input type="url" name="homepage" />
E-mail: <input type="email" name="usremail" />
<input src="submitbutton.png" type="submit" />
</form>
list 屬性的用法示範:
<fieldset>
<legend>Favorites</legend>
<p>
<label>
<input type="text" name="favorites" list="options">
<datalist id="options">
<option value="A">
<option value="B">
<option value="C">
</datalist>
</label>
</p>
</fieldset>
下面這個例子把各項屬性都用了一遍,可以拿去不同瀏覽器上實測對照:
<form action="demo_form.asp">
E-mail: <input type="email" name="email" autocomplete="on" />
Image: <input type="image" src="img_submit.gif" alt="Submit" width="48" height="48" />
Enter a date before 1980-01-01: <input type="date" name="bday" max="1979-12-31">
Enter a date after 2000-01-01: <input type="date" name="bday" min="2000-01-02">
Quantity (between 1 and 5): <input type="number" name="quantity" min="1" max="5" />
Select images: <input type="file" name="img" multiple="multiple" />
Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code" />
First Name: <input type="text" name="fname" placeholder="First name" />
Username: <input type="text" name="usrname" required />
Number: <input type="number" name="points" step="3" />
<input type="submit" />
<input type="submit" formaction="demo_admin.asp" value="Submit as admin" />
<input type="submit" formenctype="multipart/form-data" value="Submit as Multipart/form-data" />
<input type="submit" formmethod="post" formaction="demo_post.asp" value="Submit using POST" />
<input type="submit" formnovalidate value="Submit without validation" />
<input type="submit" formtarget="_blank" value="Submit to a new window" />
</form>
<form action="demo_form.asp" id="form1">
First name: <input type="text" name="fname" />
<input type="submit" value="Submit" />
</form>
Last name: <input type="text" name="lname" form="form1" />
雖然不是所有瀏覽器都支援全部類型,仍然鼓勵大家採用這些新類型。因為萬一瀏覽器不認識,最壞情況也只是退化成普通的 text 輸入框,不會壞掉,卻能讓新瀏覽器使用者立刻拿到更好的體驗——這是一筆穩賺的交易。