HTML5 新表單屬性
HTML5 新的表單屬性
HTML5 的 <form> 和 <input>標(biāo)簽添加了幾個(gè)新屬性.
<form>新屬性:
autocomplete
novalidate
<input>新屬性:
autocomplete
autofocus
form
formaction
formenctype
formmethod
formnovalidate
formtarget
height and width
list
min and max
multiple
pattern (regexp)
placeholder
required
step
<form> / <input> autocomplete 屬性
autocomplete 屬性規(guī)定 form 或 input 域應(yīng)該擁有自動(dòng)完成功能。
當(dāng)用戶在自動(dòng)完成域中開(kāi)始輸入時(shí),瀏覽器應(yīng)該在該域中顯示填寫的選項(xiàng)。
提示:?autocomplete 屬性有可能在 form元素中是開(kāi)啟的,而在input元素中是關(guān)閉的。
注意:?autocomplete 適用于 <form> 標(biāo)簽,以及以下類型的 <input> 標(biāo)簽:text, search, url, telephone, email, password, datepickers, range 以及 color。
實(shí)例
HTML form 中開(kāi)啟 autocomplete (一個(gè) input 字段關(guān)閉 autocomplete ):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="" autocomplete="on"> 姓名:<input type="text" name="fname"><br/> 留言<input type="text" name="content"><br/> E-mail: <input type="email" name="email" autocomplete="off"><br> <input type="submit"> </form> <p>填寫并提交表單,然后重新刷新頁(yè)面查看如何自動(dòng)填充內(nèi)容。</p> <p>注意 form的 autocomplete屬性為 "on"(開(kāi)),但是e-mail自動(dòng)為“off”(關(guān))。</p> </body> </html>
運(yùn)行程序嘗試一下
提示:某些瀏覽器中,您可能需要啟用自動(dòng)完成功能,以使該屬性生效。
<form> novalidate 屬性
novalidate 屬性的一個(gè)boolean 屬性.
novalidate 屬性規(guī)定在提交表單時(shí)不應(yīng)該驗(yàn)證 form 或 input 域。
實(shí)例
無(wú)需驗(yàn)證提交的表單數(shù)據(jù)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="" novalidate> E-mail: <input type="email" name="user_email"> <input type="submit"> </form> <p><strong>注意</strong>在 Safari 和 Internet Explorer 9 及之前的版本中不支持 novalidate 屬性。</p> </body> </html>
運(yùn)行程序嘗試一下
<input> autofocus 屬性
autofocus 屬性是一個(gè) boolean 屬性.
autofocus 屬性規(guī)定在頁(yè)面加載時(shí),域自動(dòng)地獲得焦點(diǎn)。
實(shí)例
讓 "留言" input 輸入域在頁(yè)面載入時(shí)自動(dòng)聚焦:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> 姓名: <input type="text" name="fname" ><br> 留言: <input type="text" name="content" autofocus><br> <input type="submit"> </form> <p><strong>注意:</strong> Internet Explorer 9及更早IE版本不支持input標(biāo)簽的 autofocus 屬性。</p> </body> </html>
運(yùn)行程序嘗試一下
你可以把a(bǔ)utofocus 換到 姓名?input 輸入域,對(duì)比兩次運(yùn)行結(jié)果看看有什么不一樣
<input> form 屬性
form 屬性規(guī)定輸入域所屬的一個(gè)或多個(gè)表單。
提示:如需引用一個(gè)以上的表單,請(qǐng)使用空格分隔的列表。
實(shí)例
位于form表單外的input 字段引用了 HTML form (該 input 表單仍然屬于form表單的一部分):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="" id="form1"> 姓名: <input type="text" name="fname"><br> <input type="submit" value="Submit"> </form> <p> "留言" 字段沒(méi)有在form表單之內(nèi),但它也是form表單的一部分。</p> 留言: <input type="text" name="lname" form="form1"> </body> </html>
運(yùn)行程序嘗試一下
注意: IE不支持form屬性
<input> formaction 屬性
?formaction 屬性用于描述表單提交的URL地址.
?formaction 屬性會(huì)覆蓋<form> 元素中的action屬性.
注意: ?formaction 屬性用于 type="submit" 和 type="image".
實(shí)例
以下HTML form表單包含了兩個(gè)不同地址的提交按鈕:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="demo.php"> 姓名: <input type="text" name="fname"><br> 密碼: <input type="text" name="lname"><br> <input type="submit" value="提交"><br> <input type="submit" formaction="admin.php" value="提交"> </form> </body> </html>
上面程序一個(gè)會(huì)提交到demo.php頁(yè)面,一個(gè)會(huì)提交到admin.php 頁(yè)面
<input> formenctype 屬性
formenctype 屬性描述了表單提交到服務(wù)器的數(shù)據(jù)編碼 (只對(duì)form表單中 method="post" 表單)
formenctype 屬性覆蓋 form 元素的 enctype 屬性。
主要:?該屬性與 type="submit" 和 type="image" 配合使用。
實(shí)例
第一個(gè)提交按鈕已默認(rèn)編碼發(fā)送表單數(shù)據(jù),第二個(gè)提交按鈕以 "multipart/form-data" 編碼格式發(fā)送表單數(shù)據(jù):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="" method="post"> name: <input type="text" name="fname"><br> <input type="submit" value="提交"> <input type="submit" formenctype="multipart/form-data" value="以 Multipart/form-data 提交"> </form> </body> </html>
運(yùn)行程序嘗試一下
<input> formmethod 屬性
formmethod 屬性定義了表單提交的方式。
formmethod 屬性覆蓋了 <form> 元素的的method 屬性。
注意:?該屬性可以與 type="submit" 和 type="image" 配合使用。
實(shí)例
重新定義表單提交方式實(shí)例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="" method="get"> 姓名: <input type="text" name="fname"><br> 密碼: <input type="text" name="lname"><br> <input type="submit" value="提交"> <input type="submit" formmethod="post" formaction="demo-post.php" value="使用 POST 提交"> </form> </body> </html>
上面代碼一個(gè)以get方式傳入一個(gè)頁(yè)面,一個(gè)以重新用formmethod 方式重新定義提交方式,以post方式提交到emo-post.php頁(yè)面
<input> formnovalidate 屬性
novalidate 屬性是一個(gè) boolean 屬性.
novalidate屬性描述了 <input> 元素在表單提交時(shí)無(wú)需被驗(yàn)證。
formnovalidate 屬性會(huì)覆蓋 <form> 元素的novalidate屬性.
注意:?formnovalidate 屬性與type="submit一起使用
實(shí)例
兩個(gè)提交按鈕的表單(使用與不適用驗(yàn)證 ):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> E-mail: <input type="email" name="userid"><br> <input type="submit" value="提交"><br> <input type="submit" formnovalidate="formnovalidate" value="不驗(yàn)證提交"> </form> </body> </html>
運(yùn)行程序嘗試一下
<input> formtarget 屬性
formtarget 屬性指定一個(gè)名稱或一個(gè)關(guān)鍵字來(lái)指明表單提交數(shù)據(jù)接收后的展示。
?formtarget 屬性覆蓋 <form>元素的target屬性.
注意:?formtarget 屬性與type="submit" 和 type="image"配合使用.
實(shí)例
兩個(gè)提交按鈕的表單, 在不同窗口中顯示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=" "> 作者姓名: <input type="text" name="fname"><br> 書本名: <input type="text" name="lname"><br> <input type="submit" value="正常提交"> <input type="submit" formtarget="_blank" value="提交到一個(gè)新的頁(yè)面上"> </form> </body> </html>
運(yùn)行程序嘗試一下
<input> height 和 width 屬性
height 和 width 屬性規(guī)定用于 image 類型的 <input> 標(biāo)簽的圖像高度和寬度。
注意:?height 和 width 屬性只適用于 image 類型的<input> 標(biāo)簽。
提示:圖像通常會(huì)同時(shí)指定高度和寬度屬性。如果圖像設(shè)置高度和寬度,圖像所需的空間 在加載頁(yè)時(shí)會(huì)被保留。如果沒(méi)有這些屬性, 瀏覽器不知道圖像的大小,并不能預(yù)留 適當(dāng)?shù)目臻g。圖片在加載過(guò)程中會(huì)使頁(yè)面布局效果改變 (盡管圖片已加載)。
實(shí)例
定義了一個(gè)圖像提交按鈕, 使用了 height 和 width 屬性:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br> <input type="image" src="http://img3.imgtn.bdimg.com/it/u=4163745010,2599120287&fm=21&gp=0.jpg" alt="Submit" width="100" height="80"> </form> </body> </html>
運(yùn)行程序嘗試一下
<input> list 屬性
list 屬性規(guī)定輸入域的 datalist。datalist 是輸入域的選項(xiàng)列表。
實(shí)例
在 <datalist>中預(yù)定義 <input> 值:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action="" method="post"> <input list="browsers" name=""> <datalist id="browsers"> <option value="Internet Explorer"> <option value="Firefox"> <option value="Chrome"> <option value="Opera"> <option value="Safari"> </datalist> <input type="submit"> </form> </body> </html>
運(yùn)行程序嘗試一下
<input> min 和 max 屬性
min、max 和 step 屬性用于為包含數(shù)字或日期的 input 類型規(guī)定限定(約束)。
注意:?min、max 和 step 屬性適用于以下類型的 <input> 標(biāo)簽:date pickers、number 以及 range。
實(shí)例
<input> 元素最小值與最大值設(shè)置:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> 輸入 1980-01-01 之前的日期: <input type="date" name="bday" max="1979-12-31"><br> 輸入 2000-01-01 之后的日期: <input type="date" name="bday" min="2000-01-02"><br> 數(shù)量 (在1和5之間): <input type="number" name="quantity" min="1" max="5"><br> <input type="submit"> </form> </body> </html>
運(yùn)行程序嘗試一下
注意:?Internet Explorer 9及更早IE版本,F(xiàn)irefox不支持input標(biāo)簽的 max 和 min 屬性。
注意:?在Internet Explorer 10中max 和 min屬性不支持輸入日期和時(shí)間,IE 10 不支持這些輸入類型。
<input> multiple 屬性
multiple 屬性是一個(gè) boolean 屬性.
multiple 屬性規(guī)定<input> 元素中可選擇多個(gè)值。
注意:?multiple 屬性適用于以下類型的 <input> 標(biāo)簽:email 和 file。: email, and file.
實(shí)例
上傳多個(gè)文件:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> 選擇圖片: <input type="file" name="img" multiple> <input type="submit"> </form> <p>嘗試選取一張或者多種圖片。</p> </body> </html>
運(yùn)行程序嘗試一下
<input> pattern 屬性
pattern 屬性描述了一個(gè)正則表達(dá)式用于驗(yàn)證 <input> 元素的值。
注意:pattern 屬性適用于以下類型的 <input> 標(biāo)簽: text, search, url, tel, email, 和 password.
提示:?是用來(lái)全局?title?屬性描述了模式.
提示:?您可以在我們的?JavaScript 教程中學(xué)習(xí)到有關(guān)正則表達(dá)式的內(nèi)容
實(shí)例
下面的例子顯示了一個(gè)只能包含三個(gè)字母的文本域(不含數(shù)字及特殊字符):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> 請(qǐng)輸入3位字母: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code"> <input type="submit"> </form> </body> </html>
運(yùn)行程序嘗試一下
<input> placeholder 屬性
placeholder 屬性提供一種提示(hint),描述輸入域所期待的值。
簡(jiǎn)短的提示在用戶輸入值前會(huì)顯示在輸入域上。
注意:?placeholder 屬性適用于以下類型的 <input> 標(biāo)簽:text, search, url, telephone, email 以及 password。
實(shí)例
input 字段提示文本:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> 姓名:<input type="text" name="fname" placeholder="請(qǐng)輸入你的姓名"><br> 密碼:<input type="text" name="lname" placeholder="請(qǐng)輸入你的密碼"><br> <input type="submit" value="提交"> </form> </body> </html>
運(yùn)行程序嘗試一下
<input> required 屬性
required 屬性是一個(gè) boolean 屬性.
required 屬性規(guī)定必須在提交之前填寫輸入域(不能為空)。
注意:required 屬性適用于以下類型的 <input> 標(biāo)簽:text, search, url, telephone, email, password, date pickers, number, checkbox, radio 以及 file。
實(shí)例
不能為空的input字段:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> 姓名: <input type="text" name="usrname" required> <input type="submit"> </form> </body> </html>
運(yùn)行程序,不填寫提交看看
<input> step 屬性
step 屬性為輸入域規(guī)定合法的數(shù)字間隔。
如果 step="3",則合法的數(shù)是 -3,0,3,6 等
提示:?step 屬性可以與 max 和 min 屬性創(chuàng)建一個(gè)區(qū)域值.
注意:?step 屬性與以下type類型一起使用: number, range, date, datetime, datetime-local, month, time 和 week.
實(shí)例
規(guī)定input step步長(zhǎng)為3:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <form action=""> <input type="number" name="points" step="3"> <input type="submit"> </form> </body> </html>
運(yùn)行程序嘗試一下
注意: Internet Explorer 9及更早IE版本,或Firefox不支持input標(biāo)簽的 step 屬性。
HTML5 <input> 標(biāo)簽
? ? ?標(biāo)簽 | ? ? ?描述 |
? ??<form> | 定義一個(gè)form表單 |
? ??<input> | 定義一個(gè) input 域 |