例
HTML表單用兩種不同的輸入類型; 文本並提交:
<form action="demo_form.asp">
Username: <input type="text" name="usrname"><br>
<input type="submit" value="Submit">
</form>
試一試» 更多“試一試”的例子。
定義和用法
該type屬性指定類型<input>元素來顯示。
默認的類型是: text 。
提示:這是不是一個必需的屬性,但我們認為你應該總是包括它。
瀏覽器支持
屬性 | |||||
---|---|---|---|---|---|
type | 是 | 是 | 是 | 是 | 是 |
注:該type屬性在所有主要瀏覽器上運行。 然而,並非所有不同的輸入類型的作品在所有主要瀏覽器。
看看下面來看看為每個輸入類型的瀏覽器的支持。
HTML 4.01和HTML5之間的差異
HTML5具有以下新的輸入類型: color, date, datetime, datetime-local, month, week, time, email, number, range, search, tel和url 。
句法
<input type="value">
屬性值
值 | 描述 |
---|---|
button | 定義一個可點擊的按鈕(大多是一個JavaScript用於激活腳本) |
checkbox | 定義一個複選框 |
color | 定義顏色選擇器 |
date | 定義日期控制(年,月,日(沒時間)) |
datetime | 輸入類型日期時間已經從HTML標準除去。 使用日期時間本地來代替。 |
datetime-local | 限定日期和時間的控制(年,月,日,時,分,秒,和級分的第二(沒有時間區的) |
定義一個字段的電子郵件地址 | |
file | 定義一個文件選擇字段和一個“瀏覽”按鈕(文件上傳) |
hidden | 定義一個隱藏的輸入字段 |
image | 定義圖像作為提交按鈕 |
month | 定義月份和年份控制(無時區) |
number | 定義一個字段輸入一個數字 |
password | 定義密碼字段(字符屏蔽) |
radio | 定義一個單選按鈕 |
range | 定義輸入一個數字其確切值的控制並不重要(如滑塊控件) |
reset | 定義一個復位按鈕(重置所有表單值默認值) |
search | 定義文本字段用於輸入搜索字符串 |
submit | 定義一個提交按鈕 |
tel | 定義了一個字段用於輸入電話號碼 |
text | 默認。 定義單行文本字段(默認寬度為20個字符) |
time | 定義一個控制輸入時間(無時區) |
url | 定義一個字段輸入URL |
week | 定義一個星期,一年控制(無時區) |
例子
輸入類型: button
輸入類型: checkbox
例
複選框讓用戶選擇的選擇數量有限的一個或多個選項:
<input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle2" value="Car"> I have a car
試一試» 輸入類型: color
輸入類型: date
輸入類型: datetime
輸入類型日期時間已經從HTML標準除去。 使用日期時間本地來代替。
輸入類型: datetime-local
輸入類型: email
提示:iPhone上的Safari識別郵件類型,並改變屏幕上的鍵盤來匹配它(添加@和.com選項)。
輸入類型: file
輸入類型: hidden
例
定義一個隱藏字段(對用戶不可見)。
隱藏字段通常存儲的默認值,或者可以有它的價值由一個JavaScript修改:
<input type="hidden" name="country" value="Norway">
試一試» 輸入類型: image
輸入類型: month
輸入類型: number
例
定義一個字段輸入一個數字(也可以設置在接受什麼數字限制):
Quantity (between 1 and 5): <input type="number" name="quantity" min="1"
max="5">
試一試» 使用following屬性來指定限制:
輸入類型: password
輸入類型: radio
例
單選按鈕讓使用者僅選擇一個選擇有限數量之一:
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other">
Other
試一試» 輸入類型: range
例
定義輸入一個數字,其精確值(如滑塊控件)並不重要控制。 您還可以設置什麼號碼被接受的限制:
<input type="range" name="points" min="0" max="10">
試一試» 使用following屬性來指定限制:
輸入類型: reset
提示:小心使用復位按鈕!它可以是惱人誰不小心激活復位按鈕的用戶。
輸入類型: search
輸入類型: submit
輸入類型: tel
輸入類型: text
例
定義兩個用戶可以輸入文本單行文本字段:
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
試一試» 輸入類型: time
輸入類型: url
提示:iPhone上的Safari識別URL輸入類型,並改變屏幕上的鍵盤來匹配它(添加.COM選項)。