CodeGym /Javaコース /Frontend SELF JA /フォーム要素のグループ化

フォーム要素のグループ化

Frontend SELF JA
レベル 8 , レッスン 4
使用可能

4.1 <fieldset>要素

フォーム要素のグループ化は、関連する要素を論理的にまとめることができるので、フォームの構造を改善し、ユーザーにとってより使いやすくなります。HTMLでは、フォーム要素をグループ化するために2つのタグを提供しています: <fieldset><legend>

<fieldset>要素は、<form>内で関連するフォーム要素をグループ化するために使用されます。視覚的には、この要素は通常、グループ化された要素の周りに枠として表示され、ユーザーにどのフィールドが1つのグループに属しているかを理解させるのに役立ちます。

使用例:

この例では、2つのフォーム要素のグループが表示されています:"Personal Information"と "Account Details"。それぞれが<fieldset>要素で囲まれており、<legend>でタイトルが付けられています。

HTML
    
      <form action="/submit" method="post">
        <fieldset>
          <legend>Personal Information</legend>
          <label for="name">Name:</label>
          <input type="text" id="name" name="name" required>
          <label for="email">Email:</label>
          <input type="email" id="email" name="email" required>
        </fieldset>
        <fieldset>
          <legend>Account Details</legend>
          <label for="username">Username:</label>
          <input type="text" id="username" name="username" required>
          <label for="password">Password:</label>
          <input type="password" id="password" name="password" required>
        </fieldset>
        <button type="submit" id="submit">Submit</button>
      </form>
      <script>
        const inputs = document.getElementsByTagName("input");
        const submit = document.getElementById("submit");

        submit.addEventListener("click", (e) => {
          e.preventDefault();

          const values = [];
          let allValid = true;

          for (let input of inputs) {
            if (!input.validity.valid) {
              allValid = false;
              break;
            }
            values.push(input.value);
          }

          if (allValid) {
            alert(
              "Personal Information:\n" +
              `Name: ${values[0]}\n` +
              `Email: ${values[1]}\n\n` +
              "Account Details:\n" +
              `Username: ${values[2]}\n` +
              `Password: ${values[3]}`
            );
          } else {
            alert("すべてのフィールドを入力してください");
          }
        });
      </script>
    
  
HTML
    
      <form>
        <fieldset>
          <legend>Personal Information</legend>
          <label for="name">Name:</label>
          <input type="text" id="name" name="name">
          <label for="email">Email:</label>
          <input type="email" id="email" name="email">
        </fieldset>
        <fieldset>
          <legend>Account Details</legend>
          <label for="username">Username:</label>
          <input type="text" id="username" name="username">
          <label for="password">Password:</label>
          <input type="password" id="password" name="password">
        </fieldset>
        <input type="submit" value="Submit">
      </form>
    
  

<fieldset>の主な属性:

disabled属性: <fieldset>内のすべての要素をユーザーがアクセスできないようにします (変更はできず、サーバーに送信されません)。

HTML
    
        <fieldset disabled>
          <legend>Disabled Group</legend>
          <label for="disabled-field">このフィールドは無効です:</label>
          <input type="text" id="disabled-field" name="disabled-field">
        </fieldset>
    
  

4.2 <legend>要素

<legend>要素は、<fieldset>を使って作成される要素のグループにタイトルを追加するために使用されます。 それは、ユーザーにフィールドグループの目的とコンテキストを理解させるのに役立ちます。

使用例:

HTML
    
        <fieldset>
          <legend>Contact Information</legend>
          <label for="phone">Phone Number:</label>
          <input type="tel" id="phone" name="phone">
          <label for="address">Address:</label>
          <input type="text" id="address" name="address">
        </fieldset>
    
  

<legend>要素の特徴:

  1. 位置: <legend>要素は常に<fieldset>内に配置され、 その位置は通常、枠の上部にあります。
  2. テキスト: <legend>内のテキストはフォームフィールドのグループを説明し、ユーザーがフォーム内を迅速にナビゲートするのに役立ちます。

<fieldset> と <legend> 使用に関するヒント

  • 論理的グループ化: <fieldset><legend> を使用してフォーム要素を論理的にグループ化することで、 フォームをより整理され、ユーザーにとって理解しやすいものにします。
  • アクセシビリティ: <fieldset><legend> 要素は、支援技術を利用するユーザーにフォームの構造と内容を理解させることで、フォームのアクセシビリティを向上させます。
  • スタイルとデザイン: <fieldset><legend>をスタイルするためにCSSを使用して、 フォームの外観を改善します。

4.3 <fieldset> と <legend> の使用例

フィールドグループ化を使用した登録フォーム:

HTML
    
      <form form action="/submit" method="post">
        <fieldset>
          <legend>Personal Details</legend>
          <label for="fname">First Name:</label>
          <input type="text" id="fname" name="fname" required>
          <label for="lname">Last Name:</label>
          <input type="text" id="lname" name="lname" required>
        </fieldset>
        <fieldset>
          <legend>Login Information</legend>
          <label for="username">Username:</label>
          <input type="text" id="username" name="username" required>
          <label for="password">Password:</label>
          <input type="password" id="password" name="password" required>
        </fieldset>
        <button type="submit" id="submit">Submit</button>
      </form>
      <script>
        const inputs = document.getElementsByTagName("input");
        const submit = document.getElementById("submit");

        submit.addEventListener("click", (e) => {
          e.preventDefault();

          const values = [];
          let allValid = true;

          for (let input of inputs) {
            if (!input.validity.valid) {
              allValid = false;
              break;
            }
            values.push(input.value);
          }

          if (allValid) {
            alert(
              "Personal Details:\n" +
              `First name: ${values[0]}\n` +
              `Last Name: ${values[1]}\n\n` +
              "Login information:\n" +
              `Username: ${values[2]}\n` +
              `Password: ${values[3]}`
            );
          } else {
            alert("すべてのフィールドを入力してください");
          }
        });
      </script>
    
  
HTML
    
      <form>
        <fieldset>
          <legend>Personal Details</legend>
          <label for="fname">First Name:</label>
          <input type="text" id="fname" name="fname">
          <label for="lname">Last Name:</label>
          <input type="text" id="lname" name="lname">
        </fieldset>
        <fieldset>
          <legend>Login Information</legend>
          <label for="username">Username:</label>
          <input type="text" id="username" name="username">
          <label for="password">Password:</label>
          <input type="password" id="password" name="password">
        </fieldset>
        <input type="submit" value="Register">
      </form>
    
  

異なるデータタイプを含むフォーム:

HTML
    
      <form action="/auth" method="post">
        <fieldset>
          <legend>Contact Information</legend>
          <label for="email">Email:</label>
          <input type="email" id="email" name="email" required>
          <label for="phone">Phone Number:</label>
          <input type="tel" id="phone" name="phone" required>
        </fieldset>
        <fieldset>
          <legend>Preferences</legend>
          <label for="newsletter">Subscribe to newsletter:</label>
          <input type="checkbox" id="newsletter" name="newsletter">
          <label for="updates">Receive updates:</label>
          <input type="checkbox" id="updates" name="updates">
        </fieldset>
        <button type="submit" id="submit">Submit</button>
      </form>
      <script>
        const form = document.querySelector("form");
        const inputs = form.querySelectorAll("input");
        const submit = document.getElementById("submit");

        submit.addEventListener("click", (e) => {
          e.preventDefault();

          let email = "";
          let phone = "";
          let newsletter = 0;
          let updates = 0;
          let allValid = true;

          inputs.forEach((input) => {
            if (input.type === "email" && (!input.validity.valid)) {
              allValid = false;
            } else if (input.type === "tel" && (!input.validity.valid)) {
              allValid = false;
            }

            if (input.type === "email") {
              email = input.value;
            } else if (input.type === "tel") {
              phone = input.value;
            } else if (input.type === "checkbox") {
              if (input.id === "newsletter") {
                newsletter = input.checked ? 1 : 0;
              } else if (input.id === "updates") {
                updates = input.checked ? 1 : 0;
              }
            }
          });

          if (allValid) {
            alert(
              "Contact Information\n" +
              `Email: ${email}\n` +
              `Phone Number: ${phone}\n\n` +
              "Preferences\n" +
              `Subscribe to newsletter: ${newsletter}\n` +
              `Receive updates: ${updates}`
            );
          } else {
            alert("EmailとPhoneのフィールドを入力してください");
          }
        });
      </script>
    
  
HTML
    
      <form>
        <fieldset>
          <legend>Contact Information</legend>
          <label for="email">Email:</label>
          <input type="email" id="email" name="email">

          <label for="phone">Phone Number:</label>
          <input type="tel" id="phone" name="phone">
        </fieldset>
        <fieldset>
          <legend>Preferences</legend>
          <label for="newsletter">Subscribe to newsletter:</label>
          <input type="checkbox" id="newsletter" name="newsletter">
          <label for="updates">Receive updates:</label>
          <input type="checkbox" id="updates" name="updates">
        </fieldset>
        <input type="submit" value="Submit">
      </form>
    
  
CSS
    
      fieldset {
        border: 2px solid #ccc;
        padding: 20px;
      }

      legend {
        font-weight: bold;
        padding: 0 10px;
      }
    
  
コメント
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION