jQuery Mobile フォームの入力要素

❮ 前章へ 次章へ ❯

jQuery Mobile テキスト入力

入力フィールドは標準の HTML 要素でコーディングしますが、jQuery Mobile は、モバイルデバイスにとって魅力的で使いやすいようにスタイルを設定します。 新しい HTML5 の <input> タイプも使用できます:

<form method="post" action="demoform.html">
  <div class="ui-field-contain">
    <label for="fullname">Full name:</label>
    <input type="text" name="fullname" id="fullname">

    <label for="bday">Date of Birth:</label>
    <input type="date" name="bday" id="bday">

    <label for="email">E-mail:</label>
    <input type="email" name="email" id="email" placeholder="Your email..">
  </div>
</form>
Try it Yourself »

テキスト・エリア

複数行のテキスト入力には <textarea> を使用します。

注:テキスト・エリアは、テキストを入力すると行に合わせて自動的に拡大されます。

<label for="info">Additional Information:</label>
<textarea name="addinfo" id="info"></textarea>
Try it Yourself »

検索入力

input type="search" は、HTML5 で新しく導入されたもので、検索を入力するためのテキストフィールドを定義します:

<label for="search">Search:</label>
<input type="search" name="search" id="search">
Try it Yourself »

ラジオボタン

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

一連のラジオボタンを作成するには、type="radio" を持つ input と、対応するラベルを追加します。ラジオボタンを <fieldset> 要素でラップします。 また、<legend> 要素を追加し、<fieldset> のキャプションを定義することもできます。

チップ:ボタンをグループ化するには data-role="controlgroup" を使用します:

<form method="post" action="demoform.html">
  <fieldset data-role="controlgroup">
    <legend>Choose your gender:</legend>
      <label for="male">Male</label>
      <input type="radio" name="gender" id="male" value="male">
      <label for="female">Female</label>
      <input type="radio" name="gender" id="female" value="female">
  </fieldset>
</form>
Try it Yourself »

チェックボックス

チェックボックスは、ユーザに限られた数の選択肢から 1 つ以上のオプションを選択させる場合に使用します:

<form method="post" action="demoform.html">
  <fieldset data-role="controlgroup">
    <legend>Choose as many favorite colors as you'd like:</legend>
      <label for="red">Red</label>
      <input type="checkbox" name="favcolor" id="red" value="red">
      <label for="green">Green</label>
      <input type="checkbox" name="favcolor" id="green" value="green">
      <label for="blue">Blue</label>
      <input type="checkbox" name="favcolor" id="blue" value="blue">
  </fieldset>
</form>
Try it Yourself »

その他の例

ラジオボタンやチェックボックスを横にグループ化するには、data-type="horizontal" を使用します:

<fieldset data-role="controlgroup" data-type="horizontal">
Try it Yourself »

You can also wrap a field container around the <fieldset>:

<div class="ui-field-contain">
  <fieldset data-role="controlgroup">
    <legend>Choose your gender:</legend>
  </fieldset>
</div>
Try it Yourself »

いずれかのボタンを「事前選択」したい場合は、HTML <input> の checked 属性を使用します:

<input type="radio" checked>
<input type="checkbox" checked>
Try it Yourself »

フォームをポップアップの中に配置することもできます:

<a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline">Show Popup Form</a>

<div data-role="popup" id="myPopup" class="ui-content">
  <form method="post" action="demoform.html">
    <div>
      <h3>Login information</h3>
      <label for="usrnm" class="ui-hidden-accessible">Username:</label>
      <input type="text" name="user" id="usrnm" placeholder="Username">
      <label for="pswd" class="ui-hidden-accessible">Password:</label>
      <input type="password" name="passw" id="pswd" placeholder="Password">
    </div>
  </form>
</div>
Try it Yourself »


❮ 前章へ 次章へ ❯