最新的Web開發教程
 

HTML <input> type Attribute

<HTML <input>標籤

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, telurl


句法

<input type="value">

屬性值

描述
button 定義一個可點擊的按鈕(大多是一個JavaScript用於激活腳本)
checkbox 定義一個複選框
color 定義顏色選擇器
date 定義日期控制(年,月,日(沒時間))
datetime 輸入類型日期時間已經從HTML標準除去。 使用日期時間本地來代替。
datetime-local 限定日期和時間的控制(年,月,日,時,分,秒,和級分的第二(沒有時間區的)
email 定義一個字段的電子郵件地址
file 定義一個文件選擇字段和一個“瀏覽”按鈕(文件上傳)
hidden 定義一個隱藏的輸入字段
image 定義圖像作為提交按鈕
month 定義月份和年份控制(無時區)
number 定義一個字段輸入一個數字
password 定義密碼字段(字符屏蔽)
radio 定義一個單選按鈕
range 定義輸入一個數字其確切值的控制並不重要(如滑塊控件)
reset 定義一個復位按鈕(重置所有表單值默認值)
search 定義文本字段用於輸入搜索字符串
submit 定義一個提交按鈕
tel 定義了一個字段用於輸入電話號碼
text 默認。 定義單行文本字段(默認寬度為20個字符)
time 定義一個控制輸入時間(無時區)
url 定義一個字段輸入URL
week 定義一個星期,一年控制(無時區)

例子

例子

輸入類型: button

OperaSafariChromeFirefoxInternet Explorer / Edge

一個可點擊的按鈕,激活一個JavaScript被點擊時它:

<input type="button" value="Click me" onclick="msg()">
試一試»

輸入類型: checkbox

OperaSafariChromeFirefoxInternet Explorer / Edge

複選框讓用戶選擇的選擇數量有限的一個或多個選項:

<input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle2" value="Car"> I have a car
試一試»

輸入類型: color

OperaSafariChromeFirefoxInternet Explorer / Edge

從顏色選擇器顏色:

Select your favorite color: <input type="color" name="favcolor">
試一試»

輸入類型: date

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個日期控件:

Birthday: <input type="date" name="bday">
試一試»

輸入類型: datetime

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個日期和時間的控制(與時區):

Birthday (date and time): <input type="datetime" name="bdaytime">
試一試»

輸入類型日期時間已經從HTML標準除去。 使用日期時間本地來代替。


輸入類型: datetime-local

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個日期和時間的控制(沒有時區):

Birthday (date and time): <input type="datetime-local" name="bdaytime">
試一試»

輸入類型: email

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個字段為電子郵件地址(提交時會自動驗證):

E-mail: <input type="email" name="usremail">
試一試»

提示:iPhone上的Safari識別郵件類型,並改變屏幕上的鍵盤來匹配它(添加@和.com選項)。


輸入類型: file

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個文件選擇字段和一個“瀏覽”按鈕(文件上傳):

Select a file: <input type="file" name="img">
試一試»

輸入類型: hidden

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個隱藏字段(對用戶不可見)。

隱藏字段通常存儲的默認值,或者可以有它的價值由一個JavaScript修改:

<input type="hidden" name="country" value="Norway">
試一試»

輸入類型: image

OperaSafariChromeFirefoxInternet Explorer / Edge

定義圖像作為一個提交按鈕:

<input type="image" src="img_submit.gif" alt="Submit">
試一試»

輸入類型: month

OperaSafariChromeFirefoxInternet Explorer / Edge

定義月份和年份控制(無時區):

Birthday (month and year): <input type="month" name="bdaymonth">
試一試»

輸入類型: number

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個字段輸入一個數字(也可以設置在接受什麼數字限制):

Quantity (between 1 and 5): <input type="number" name="quantity" min="1" max="5">
試一試»

使用following屬性來指定限制:

  • max -指定允許的最大值
  • min -指定允許的最小值
  • step -指定合法的數字間隔
  • value -指定缺省值

輸入類型: password

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個密碼字段(字符屏蔽)

<input type="password" name="pwd">
試一試»

輸入類型: radio

OperaSafariChromeFirefoxInternet Explorer / Edge

單選按鈕讓使用者僅選擇一個選擇有限數量之一:

<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

OperaSafariChromeFirefoxInternet Explorer / Edge

定義輸入一個數字,其精確值(如滑塊控件)並不重要控制。 您還可以設置什麼號碼被接受的限制:

<input type="range" name="points" min="0" max="10">
試一試»

使用following屬性來指定限制:

  • max -指定允許的最大值
  • min -指定允許的最小值
  • step -指定合法的數字間隔
  • value -指定缺省值

輸入類型: reset

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個復位按鈕(重置所有形式的值設置為默認值):

<input type="reset">
試一試»

提示:小心使用復位按鈕!它可以是惱人誰不小心激活復位按鈕的用戶。


輸入類型: search

OperaSafariChromeFirefoxInternet Explorer / Edge

定義搜索字段(如網站搜索或谷歌搜索):

Search Google: <input type="search" name="googlesearch">
試一試»

輸入類型: submit

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個提交按鈕:

<input type="submit">
試一試»

輸入類型: tel

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個字段輸入電話號碼:

Telephone: <input type="tel" name="usrtel">
試一試»

輸入類型: text

OperaSafariChromeFirefoxInternet Explorer / Edge

定義兩個用戶可以輸入文本單行文本字段:

First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
試一試»

輸入類型: time

OperaSafariChromeFirefoxInternet Explorer / Edge

定義輸入時間(無時區)的控制:

Select a time: <input type="time" name="usr_time">
試一試»

輸入類型: url

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個字段輸入URL:

Add your homepage: <input type="url" name="homepage">
試一試»

提示:iPhone上的Safari識別URL輸入類型,並改變屏幕上的鍵盤來匹配它(添加.COM選項)。


輸入類型: week

OperaSafariChromeFirefoxInternet Explorer / Edge

定義一個星期,一年控制(無時區):

Select a week: <input type="week" name="week_year">
試一試»

<HTML <input>標籤