HTML <input>タイプ属性

❮HTML<input>タグ

2つの入力フィールドを持つHTMLフォーム。1つのテキストフィールドと1つの送信ボタン:

<form action="/action_page.php">
  <label for="username">Username: </label>
  <input type="text" id="username" name="username"><br>
  <input type="submit" value="Submit">
</form>

以下の「自分で試してみてください」の例をもっと見てください。


定義と使用法

このtype属性は、 <input>表示する要素のタイプを指定します。

属性が指定されていない場合type、デフォルトのタイプは「テキスト」です。


ブラウザのサポート

Attribute
type Yes Yes Yes Yes Yes

構文

<input type="value">

属性値

Value Description
button Defines a clickable button (mostly used with a JavaScript to activate a script)
checkbox Defines a checkbox
color Defines a color picker
date Defines a date control (year, month, day (no time))
datetime-local Defines a date and time control (year, month, day, time (no timezone)
email Defines a field for an e-mail address
file Defines a file-select field and a "Browse" button (for file uploads)
hidden Defines a hidden input field
image Defines an image as the submit button
month Defines a month and year control (no timezone)
number Defines a field for entering a number
password Defines a password field
radio Defines a radio button
range Defines a range control (like a slider control)
reset Defines a reset button
search Defines a text field for entering a search string
submit Defines a submit button
tel Defines a field for entering a telephone number
text Default. Defines a single-line text field
time Defines a control for entering a time (no timezone)
url Defines a field for entering a URL
week Defines a week and year control (no timezone)


その他の例

入力タイプ:ボタン

クリックするとJavaScriptがアクティブになるプッシュボタン:

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

入力タイプ:チェックボックス

チェックボックスを使用すると、ユーザーは限られた数の選択肢から1つ以上のオプションを選択できます。

<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label><br>

入力タイプ:色

カラーピッカーから色を選択します。

<label for="favcolor">Select your favorite color:</label>
<input type="color" id="favcolor" name="favcolor">

入力タイプ:日付

日付コントロールを定義します。

<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">

入力タイプ:datetime-local

日付と時刻の制御を定義します(タイムゾーンなし):

<label for="birthdaytime">Birthday (date and time):</label>
<input type="datetime-local" id="birthdaytime" name="birthdaytime">

入力タイプ:メール

電子メールアドレスのフィールドを定義します(送信時に自動的に検証されます)。

<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">

入力タイプ:ファイル

ファイル選択フィールドと[参照...]ボタン(ファイルアップロード用)を定義します。

<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">

入力タイプ:非表示

非表示フィールドを定義します(ユーザーには表示されません)。

非表示フィールドには、フォームの送信時に更新する必要のあるデータベースレコードが格納されることがよくあります。

<input type="hidden" id="custId" name="custId" value="3487">

入力タイプ:画像

画像を送信ボタンとして定義します。

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

入力タイプ:月

月と年のコントロールを定義します(タイムゾーンなし):

<label for="bdaymonth">Birthday (month and year):</label>
<input type="month" id="bdaymonth" name="bdaymonth">

入力タイプ:数値

番号を入力するためのフィールドを定義します(受け入れる番号に制限を設定することもできます)。

<label for="quantity">Quantity (between 1 and 5):</label>
<input type="number" id="quantity" name="quantity" min="1" max="5">

次の属性を使用して、制限を指定します。

  • max-許可される最大値を指定します
  • min-許可される最小値を指定します
  • step-有効な番号間隔を指定します
  • value-デフォルト値を指定します

入力タイプ:パスワード

パスワードフィールドを定義します(文字はマスクされます):

<label for="pwd">Password:</label>
<input type="password" id="pwd" name="pwd">

入力タイプ:ラジオ

ラジオボタンを使用すると、ユーザーは限られた数の選択肢から1つだけを選択できます。

<input type="radio" id="html" name="fav_language" value="HTML">
<label for="html">HTML</label><br>
<input type="radio" id="css" name="fav_language" value="CSS">
<label for="css">CSS</label><br>
<input type="radio" id="javascript" name="fav_language" value="JavaScript">
<label for="javascript">JavaScript</label>

入力タイプ:範囲

正確な値が重要ではない数値を入力するためのコントロールを定義します(スライダーコントロールなど)。デフォルトの範囲は0〜100です。ただし、最小、最大、およびステップ属性で受け入れる数値に制限を設定できます。

<label for="vol">Volume (between 0 and 50):</label>
<input type="range" id="vol" name="vol" min="0" max="50">

次の属性を使用して、制限を指定します。

  • max-許可される最大値を指定します
  • min-許可される最小値を指定します
  • step-有効な番号間隔を指定します
  • value-デフォルト値を指定します

入力タイプ:リセット

リセットボタンを定義します(すべてのフォーム値をデフォルト値にリセットします)。

<input type="reset">

ヒント:リセットボタンは慎重に使用してください。誤ってリセットボタンをアクティブにしたユーザーにとっては、煩わしい場合があります。

入力タイプ:検索

検索フィールドを定義します(サイト検索やGoogle検索など)。

<label for="gsearch">Search Google:</label>
<input type="search" id="gsearch" name="gsearch">

入力タイプ:送信

送信ボタンを定義します。

<input type="submit">

入力タイプ:tel

電話番号を入力するためのフィールドを定義します。

<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">

入力タイプ:テキスト

ユーザーがテキストを入力できる2つの単一行テキストフィールドを定義します。

<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br>

入力タイプ:時間

時間(タイムゾーンなし)を入力するためのコントロールを定義します。

<label for="appt">Select a time:</label>
<input type="time" id="appt" name="appt">

入力タイプ:url

URLを入力するためのフィールドを定義します。

<label for="homepage">Add your homepage:</label>
<input type="url" id="homepage" name="homepage">

ヒント: iPhoneのSafariは、URL入力タイプを認識し、それに合わせて画面キーボードを変更します(.comオプションを追加します)。

入力タイプ:週

週と年の管理を定義します(タイムゾーンなし):

<label for="week">Select a week:</label>
<input type="week" id="week" name="week">

❮HTML<input>タグ