CodeGym /Java 课程 /Frontend SELF ZH /表单元素的分组

表单元素的分组

Frontend SELF ZH
第 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