標籤:
HTML Input 屬性value 屬性
value 屬性規定輸入欄位的初始值:
readonly 屬性
readonly 屬性規定輸入欄位為唯讀(不能修改):
readonly 屬性不需要值。它等同於 readonly="readonly"。
執行個體
disabled 屬性
disabled 屬性規定輸入欄位是禁用的。
被禁用的元素是不可用和不可點擊的。
被禁用的元素不會被提交。
執行個體
size 屬性
size 屬性規定輸入欄位的尺寸(以字元計):
執行個體
maxlength 屬性
maxlength 屬性規定輸入欄位允許的最大長度:
如設定 maxlength 屬性,則輸入控制項不會接受超過所允許數的字元。
該屬性不會提供任何反饋。如果需要提醒使用者,則必須編寫 JavaScript 代碼。
注釋:輸入限制並非萬無一失。JavaScript 提供了很多方法來增加非法輸入。如需安全地限制輸入,則接受者(伺服器)必須同時對限制進行檢查。
執行個體
HTML5 屬性
HTML5 為 <input> 增加了如下屬性:
- autocomplete
- autofocus
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- height 和 width
- list
- min 和 max
- multiple
- pattern (regexp)
- placeholder
- required
- step
autocomplete 屬性
autocomplete 屬性規定表單或輸入欄位是否應該自動完成。
當自動完成開啟,瀏覽器會基於使用者之前的輸入值自動填寫值。
提示:
Tip: It is possible to have autocomplete "on" for the form、and "off" for specific input fields、or vice versa.
autocomplete 屬性適用於 <form> 以及如下 <input> 類型:text、search、url、tel、email、password、datepickers、range 以及 color。
Opera Safari Chrome Firefox Internet Explorer
執行個體
自動完成開啟的 HTML 表單(某個輸入欄位為 off):
並為 <form> 增加如需屬性:
novalidate 屬性
novalidate 屬性屬於 <form> 屬性。
如果設定,則 novalidate 規定在提交表單時不對錶單資料進行驗證。
Opera Safari Chrome Firefox Internet Explorer
執行個體
指示表單在被提交時不進行驗證:
<form action="action_page.php" novalidate> E-mail: <input type="email" name="user_email"> <input type="submit"></form>
autofocus 屬性
autofocus 屬性是布爾屬性。
如果設定,則規定當頁面載入時 <input> 元素應該自動獲得焦點。
Opera Safari Chrome Firefox Internet Explorer
執行個體
使 "First name" 輸入欄位在頁面載入時自動獲得焦點:
form 屬性
form 屬性規定 <input> 元素所屬的一個或多個表單。
提示:如需引用一個以上的表單,請使用空格分隔的表單 id 列表。
Opera Safari Chrome Firefox Internet Explorer
執行個體
輸入欄位位於 HTML 表單之外(但仍屬表單):
formaction 屬性
formaction 屬性規定當提交表單時處理該輸入控制項的檔案的 URL。
formaction 屬性覆蓋 <form> 元素的 action 屬性。
formaction 屬性適用於 type="submit" 以及 type="image"。
Opera Safari Chrome Firefox Internet Explorer
執行個體
擁有兩個兩個提交按鈕並對於不同動作的 HTML 表單:
formenctype 屬性
formenctype 屬性規定當把表單資料(form-data)提交至伺服器時如何對其進行編碼(僅針對 method="post" 的表單)。
formenctype 屬性覆蓋 <form> 元素的 enctype 屬性。
formenctype 屬性適用於 type="submit" 以及 type="image"。
Opera Safari Chrome Firefox Internet Explorer
執行個體
發送預設編碼以及編碼為 "multipart/form-data"(第二個提交按鈕)的表單資料(form-data):
<form action="demo_post_enctype.asp" method="post"> First name: <input type="text" name="fname"><br> <input type="submit" value="Submit"> <input type="submit" formenctype="multipart/form-data" value="Submit as Multipart/form-data"></form>
formmethod 屬性
formmethod 屬性定義用以向 action URL 發送表單資料(form-data)的 HTTP 方法。
formmethod 屬性覆蓋 <form> 元素的 method 屬性。
formmethod 屬性適用於 type="submit" 以及 type="image"。
Opera Safari Chrome Firefox Internet Explorer
執行個體
第二個提交按鈕覆蓋表單的 HTTP 方法:
<form action="action_page.php" method="get"> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br> <input type="submit" value="Submit"> <input type="submit" formmethod="post" formaction="demo_post.asp" value="Submit using POST"></form>
formnovalidate 屬性
novalidate 屬性是布爾屬性。
如果設定,則規定在提交表單時不對 <input> 元素進行驗證。
formnovalidate 屬性覆蓋 <form> 元素的 novalidate 屬性。
formnovalidate 屬性可用於 type="submit"。
Opera Safari Chrome Firefox Internet Explorer
執行個體
擁有兩個提交按鈕的表單(驗證和不驗證):
formtarget 屬性
formtarget 屬性規定的名稱或關鍵詞指示提交表單後在何處顯示接收到的響應。
formtarget 屬性會覆蓋 <form> 元素的 target 屬性。
formtarget 屬性可與 type="submit" 和 type="image" 使用。
Opera Safari Chrome Firefox Internet Explorer
執行個體
這個表單有兩個提交按鈕,對應不同的目標視窗:
height 和 width 屬性
height 和 width 屬性規定 <input> 元素的高度和寬度。
height 和 width 屬性僅用於 <input type="image">。
注釋:請始終規定映像的尺寸。如果瀏覽器不清楚映像尺寸,則頁面會在映像載入時閃爍。
Opera Safari Chrome Firefox Internet Explorer
執行個體
把映像定義為提交按鈕,並設定 height 和 width 屬性:
list 屬性
list 屬性引用的 <datalist> 元素中包含了 <input> 元素的預定義選項。
Opera Safari Chrome Firefox Internet Explorer
執行個體
使用 <datalist> 設定預定義值的 <input> 元素:
datalist與select(下拉式清單)的區別:
select:5個值裡面選擇1個;
datalist:你可以在文字框裡填值,也可以在下面5個值裡選1個。
min 和 max 屬性
min 和 max 屬性規定 <input> 元素的最小值和最大值。
min 和 max 屬性適用於如需輸入類型:number、range、date、datetime、datetime-local、month、time 以及 week。
Opera Safari Chrome Firefox Internet Explorer
執行個體
具有最小和最大值的 <input> 元素:
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">
multiple 屬性
multiple 屬性是布爾屬性。可以選擇檔案!
如果設定,則規定允許使用者在 <input> 元素中輸入一個以上的值。
multiple 屬性適用於以下輸入類型:email 和 file。
Opera Safari Chrome Firefox Internet Explorer
執行個體
接受多個值的檔案上傳欄位:
pattern 屬性
pattern 屬性規定用於檢查 <input> 元素值的Regex。
pattern 屬性適用於以下輸入類型:text、search、url、tel、email、and password。
提示:請使用全域的 title 屬性對模式進行描述以協助使用者。
提示:請在我們的 JavaScript 教程中學習更多有關Regex的知識。
Opera Safari Chrome Firefox Internet Explorer
執行個體
只能包含三個字母的輸入欄位(無數字或特殊字元)
placeholder 屬性
placeholder 屬性規定用以描述輸入欄位預期值的提示(樣本值或有關格式的簡短描述)。
該提示會在使用者輸入值之前顯示在輸入欄位中。
placeholder 屬性適用於以下輸入類型:text、search、url、tel、email 以及 password。
Opera Safari Chrome Firefox Internet Explorer
執行個體
擁有預留位置文字的輸入欄位:
required 屬性
required 屬性是布爾屬性。
如果設定,則規定在提交表單之前必須填寫輸入欄位。
required 屬性適用於以下輸入類型:text、search、url、tel、email、password、date pickers、number、checkbox、radio、and file.
Opera Safari Chrome Firefox Internet Explorer
執行個體
必填的輸入欄位:
step 屬性
step 屬性規定 <input> 元素的合法數字間隔。
樣本:如果 step="3",則合法數字應該是 -3、0、3、6、等等。
提示:step 屬性可與 max 以及 min 屬性一同使用,來建立合法值的範圍。
step 屬性適用於以下輸入類型:number、range、date、datetime、datetime-local、month、time 以及 week。
Opera Safari Chrome Firefox Internet Explorer
樣本
擁有具體的合法數字間隔的輸入欄位:
練習的代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- form表單用於定義表單,而table用於有序的布局文字框等組件 -->
<form>
<fieldset>
<legend>註冊及使用者登入頁面</legend>
<table>
<tr>
<td><input type="radio" name="sex" value="male">男生<br/></td>
</tr>
<tr>
<td><input type="radio" name="sex" value="female">女生<br/></td>
</tr>
<tr>
<td>使用者名稱:</td>
<td><input type="text" name="username" value="張三" disabled></td>
<td><input type="text" name="asdf" placeholder="jack chen" required="required" /></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="passwd" autofocus="autofocus" ></td>
</tr>
<tr>
<td><input type="button" name="submit" value="提交"></td>
<td><input type="button" name="reset" value="重設"></td>
</tr>
<tr>
<select name="country">
<option value="America" selected>美國</option>
<option value="China" >中國</option>
<option value="Korea">韓國</option>
</select>
</tr>
</table>
</fieldset>
</form>
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
<br><hr>
Enter a date after 1997-3-2:<br/>
<input type="date" name="d1" min="1997-3-2">
<input type="submit"><br>
<input type="range" name="rag1" min="1" max="10" ><br>
<input type="month" name="m1"><br>
<input type="week" name="w1"><br>
<input type="time" name="t1"><br>
<input type="datetime" name="dt1"><br>
<input type="datetime-local" name="dt-l1"><br>
<input type="tel" name="tel1"><br>
<input type="file" name="img" multiple="multiple"><br>
</body>
</html>
結果:
HTML Input屬性