DIV CSS 佈局教程網

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

在 HTML5 增強的眾多元素中,最值得投入注意力的一定是表單。表單在這次改版中幾乎被翻新了一遍,過去必須靠 JavaScript 手寫的一堆互動與驗證功能,現在單靠標籤屬性就能搞定。

開始之前,先記住一個關鍵觀念:

在 HTML5 中,表單控制項可以位於它所屬表單的外部。因此 fieldset、label、input 等元素都新增了 form 屬性,用來標示自己歸屬於哪一個表單。

form 元素:兩個新屬性

  • autocomplete:啟用「下拉建議列表」功能。
  • novalidate:關閉表單驗證,除錯測試時非常實用。

fieldset 元素:三個新屬性

  • disabled:整體停用該 fieldset 內的控制項。
  • name:為 fieldset 設定名稱。
  • form:值為所屬表單的 ID,可填多個。當 fieldset 被移到表單外部時,必須靠這個屬性把兩者正確關聯起來。

label 元素:form 屬性與容易被忽略的 for

除了新增 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」,後面的輸入框就會獲得焦點。

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 (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>

input 新增屬性一覽

  • autocomplete:自動帶出先前輸入過的內容,取值 on 或 off。適用於 text、search、url、tel、email、password、date pickers、range、color。
  • autofocus:頁面載入完成後自動取得焦點。
  • form:指定所屬 form,可多個。
  • formaction:指定提交後負責處理的頁面或檔案。
  • formenctype:指定提交資料的編碼方式。
  • formmethod:指定送出表單的 HTTP 方法,會覆蓋 form 上的設定。
  • formnovalidate:提交前不檢查資料有效性。
  • formtarget:指定回應內容的顯示位置。
  • height / width:輸入框的高與寬,僅適用 image 類型。
  • max / min:允許的最大值與最小值,適用 number、range 與日期類型。
  • multiple:是否允許輸入多個值,適用 email 與 file。
  • pattern:用來驗證輸入值的正則表達式,適用 text、search、url、tel、email、password。
  • placeholder:輸入前的提示文字,適用 text、search、url、tel、email、password。
  • required:是否為必填,未填則表單無法提交,適用 text、search、url、tel、email、password、date pickers、number、checkbox、radio、file。
  • step:自動增減時的步長,適用 number、range、date、datetime、datetime-local、month、time、week。
  • list:搭配 datalist 元素提供候選清單,適用 text、search、url、tel、email、date、number、range、color。

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 輸入框,不會壞掉,卻能讓新瀏覽器使用者立刻拿到更好的體驗——這是一筆穩賺的交易。

實用參考

  • W3C 教程: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/
  • 前端博客:https://www.pjhome.net/default.asp?cateID=1
  • JS 操作表單相關知識:https://www.cnblogs.com/xugang/archive/2010/08/12/1798005.html
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved