CodeGym /Java Adesua /Frontend SELF TW /表單元素分組

表單元素分組

Frontend SELF TW
等級 8 , 課堂 4
開放

4.1 元素 <fieldset>

表單元素的分組可以邏輯上將相關的元素集中在一起,這有助於改善表單的結構,讓使用者更方便操作。HTML 提供了兩個用於分組表單元素的標籤: <fieldset><legend>

元素 <fieldset> 用於將相關的表單元素在 <form> 中分組。在視覺上, 這個元素通常顯示為一個框架圍繞分組的元素,以幫助使用者理解哪些字段屬於同一組。

使用範例:

在這個例子中,我們看到兩組表單元素:"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">This field is disabled:</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> 元素提升了表單的可獲得性,幫助使用輔助技術的使用者理解表單的結構與內容
  • 樣式和設計: 使用 CSS 來美化 <fieldset><legend> 改善表單的外觀

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