DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 元素擴展(下):值得關注的強化表單功能
編輯:HTML5詳解     

在 HTML5 對既有元素的強化當中,改動幅度最大的莫過於表單。過去必須靠 JavaScript 才能做到的一批功能——日期挑選器、範圍滑桿、自動驗證、提示文字——現在幾乎都能純宣告式完成。以下依元素逐一梳理新增的屬性與型別。

先記住一個觀念轉變

在 HTML5 中,表單控制項可以位於它所屬表單的外部。因此 fieldset、label、input 等元素都新增了 form 屬性,用來標示自己歸屬於哪一個表單(值為表單的 id,且可以同時屬於多個)。這個特性讓版面安排自由度大幅提升,不必再為了結構而硬把控制項塞進 <form> 裡。

form 元素:autocomplete 與 novalidate

  • autocomplete:啟用「下拉建議清單」,讓瀏覽器自動帶出使用者先前輸入過的內容。
  • novalidate:關閉整個表單的驗證。開發測試階段非常好用,省去每次都要填完必填欄位的麻煩。

fieldset 元素:disabled、name、form

disabled 可一次停用群組內的所有控制項,name 用於設定群組名稱。至於 form,當 fieldset 被放到表單外面時就必須設定,這樣它才能正確與一個或多個表單關聯起來。

label 元素:別忽略 for 屬性

除了新增 form 之外,label 只有既有的 for 屬性值得留意。它用來指定該標籤所附屬的控制項 id,按下標籤文字時,對應的控制項就會獲得焦點。

<form action="demo_form.asp" id="form1">
  <label for="name">Click Me</label>
  <input id="name" type="text" />
  <input type="submit" value="Submit" />
</form>

點擊「Click Me」,後方的輸入框就會取得焦點。這個屬性對無障礙設計與行動裝置的使用體驗幫助很大,卻常被忽略。

input 的新增型別

除了原有的 button、text、submit、checkbox、radio、select、password,HTML5 一口氣加入了下列型別:

  • 顏色:color
  • 各種日期與時間:date、datetime、datetime-local、month、week、time
  • 電子郵件:email
  • 數字:number
  • 範圍:range
  • 搜尋:search
  • 電話:tel
  • 網址:url

這些型別的價值在於語意化:瀏覽器與作業系統能據此提供合適的鍵盤面板、挑選器與內建驗證。可以跑下面的範例,實際比較各瀏覽器的支援情形。

<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 (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" />
  Quantity (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 type="submit" value="送出" />
</form>

input 的新增屬性

  • autocomplete:自動帶出先前輸入的內容,值為 "on" 或 "off"。適用於 text、search、url、tel、email、password、date pickers、range 與 color。
  • autofocus:頁面載入完成後自動取得焦點。
  • form:指定所屬的 form id,可以有多個。
  • formaction:覆寫表單的提交網址。
  • formenctype:覆寫表單的編碼方式。
  • formmethod:覆寫表單的 HTTP 方法。
  • formnovalidate:提交時跳過驗證。
  • formtarget:指定回應內容要在哪裡開啟,例如 _blank。
  • height / width:輸入框的高與寬,僅適用於 image 型別。
  • max / min:可接受的最大值與最小值,適用於 number、range 與各日期型別。
  • multiple:是否允許輸入多個值,適用於 email 與 file。
  • pattern:以正則表達式驗證輸入內容,適用於 text、search、url、tel、email、password。
  • placeholder:輸入前的提示文字,適用範圍與 pattern 相同。
  • required:設為必填,未填寫則表單無法送出。適用於 text、search、url、tel、email、password、date pickers、number、checkbox、radio 與 file。
  • step:數字遞增或遞減的步長,適用於 number、range 與各日期時間型別。
  • list:搭配 datalist 元素提供候選清單,可用於 text、search、url、tel、email、date、number、range 與 color。

list 搭配 datalist 的小例子

<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="以管理者身分送出" />
  <input type="submit" formenctype="multipart/form-data"
         value="以 multipart 編碼送出" />
  <input type="submit" formmethod="post" formaction="demo_post.asp"
         value="以 POST 送出" />
  <input type="submit" formnovalidate value="不驗證直接送出" />
  <input type="submit" formtarget="_blank" value="在新視窗開啟" />
</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" />

最後一行就是前面提到的「控制項在表單外部」的實際寫法:lname 雖然寫在 <form> 之外,靠著 form="form1" 一樣會隨該表單一起送出。

寫在最後的建议

雖然不是每個瀏覽器都支援全部型別,仍然鼓勵大家使用這些新語法。關鍵在於 HTML5 的降級設計:瀏覽器若認不得某個 type,只會退化成普通的 text 輸入框,不會報錯也不會壞版面。也就是說,採用新語法的成本幾乎為零,卻能在支援的環境中直接獲得更好的體驗。

延伸資源:
W3Schools HTML5 教學:https://www.w3schools.com/html5/default.asp
HTML5 官方指導文件:https://dev.w3.org/html5/html-author/
HTML5 Doctor:https://html5doctor.com/
HTML5 中文教學:https://www.html5china.com/

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved