HTML <input> 標籤


實例

一個簡單的 HTML 表單,包含兩個文本輸入框和一個提交按鈕:

<form action="demo_form.asp">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="提交">
</form>


(本頁底部可以查看更多實例)

流覽器支持

Internet Explorer Firefox Opera Google Chrome Safari

目前大多數流覽器支持 <input>標籤。


標籤定義及使用說明

<input> 標籤規定了用戶可以在其中輸入數據的輸入字段。

<input> 元素在 <form> 元素中使用,用來聲明允許用戶輸入數據的 input 控件。

輸入字段可通過多種方式改變,取決於 type 屬性。


提示和注釋

注意: <input> 元素是空的,它只包含標籤屬性。

提示: 你可以使用 <label> 元素來定義 <input> 元素的標注。


HTML 4.01 與 HTML5之間的差異

在 HTML 4.01 中, "align" 數據已經不再使用。HTML5 中不支持該屬性。 可以使用CSS來定義 <input> 元素的對齊方式。

在 HTML5中, <input> 添加了幾個屬性,並且添加了對應的值。


HTML 與 XHTML 之間的差異

在 HTML 中,<input> 標籤沒有結束標籤。

在 XHTML 中,<input> 標籤必須被正確地關閉。


屬性

New : HTML5新標籤。

屬性 描述
accept audio/* video/* image/* MIME_type 規定通過檔上傳來提交的檔的類型。 (只針對type="file")
align left right top middle bottom HTML5已廢棄,不贊成使用。規定圖像輸入的對齊方式。 (只針對type="image")
alt text 定義圖像輸入的替代文本。 (只針對type="image")
autocompleteNew on off autocomplete 屬性規定 <input> 元素輸入字段是否應該啟用自動完成功能。
autofocusNew autofocus 屬性規定當頁面加載時 <input> 元素應該自動獲得焦點。
checked checked checked 屬性規定在頁面加載時應該被預先選定的 <input> 元素。 (只針對 type="checkbox" 或者 type="radio")
disabled disabled disabled 屬性規定應該禁用的 <input> 元素。
formNew form_id form 屬性規定 <input> 元素所屬的一個或多個表單。
formactionNew URL 屬性規定當表單提交時處理輸入控件的檔的 URL。(只針對 type="submit" 和 type="image")
formenctypeNew application/x-www-form-urlencoded multipart/form-data text/plain 屬性規定當表單數據提交到伺服器時如何編碼(只適合 type="submit" 和 type="image")。
formmethodNew get post 定義發送表單數據到 action URL 的 HTTP 方法。 (只適合 type="submit" 和 type="image")
formnovalidateNew formnovalidate formnovalidate 屬性覆蓋 <form> 元素的 novalidate 屬性。
formtargetNew _blank _self _parent _top framename 規定表示提交表單後在哪里顯示接收到回應的名稱或關鍵字。(只適合 type="submit" 和 type="image")
heightNew pixels 規定 <input>元素的高度。(只針對type="image")
listNew datalist_id 屬性引用 <datalist> 元素,其中包含 <input> 元素的預定義選項。
maxNew number date 屬性規定 <input> 元素的最大值。
maxlength number 屬性規定 <input> 元素中允許的最大字符數。
minNew number date 屬性規定 <input>元素的最小值。
multipleNew multiple 屬性規定允許用戶輸入到 <input> 元素的多個值。
name text name 屬性規定 <input> 元素的名稱。
patternNew regexp pattern 屬性規定用於驗證 <input> 元素的值的正則運算式。
placeholderNew text placeholder 屬性規定可描述輸入 <input> 字段預期值的簡短的提示資訊 。
readonly readonly readonly 屬性規定輸入字段是只讀的。
requiredNew required 屬性規定必需在提交表單之前填寫輸入字段。
size number size 屬性規定以字元數計的 <input> 元素的可見寬度。
src URL src 屬性規定顯示為提交按鈕的圖像的 URL。 (只針對 type="image")
stepNew number step 屬性規定 <input> 元素的合法數字間隔。
type button
checkbox
color
date
datetime
datetime-local
email
file
hidden
image
month
number
password
radio
range
reset
search
submit
tel
text
time
url
week
type 屬性規定要顯示的 <input> 元素的類型。
value text 指定 <input> 元素 value 的值。
widthNew pixels width 屬性規定 <input> 元素的寬度。 (只針對type="image")


全局屬性

<input> 標籤支持全局屬性,查看完整屬性表 HTML全局屬性


事件屬性

<input> 標籤支持所有 HTML事件屬性