freeCodeCamp/curriculum/challenges/chinese-traditional/14-responsive-web-design-22/learn-html-forms-by-buildin.../60fad1cafcde010995e15306.md

4.5 KiB
Raw Blame History

id title challengeType dashedName
60fad1cafcde010995e15306 步驟 36 0 step-36

--description--

在提交表單時,向每個可提交的元素提供 name 屬性很有用,並且是最佳實踐。 此屬性用於表單提交時識別不同的元素。

除了兩個 radio 輸入之外,爲每個可提交元素指定一個你選擇的唯一 name 屬性。

--hints--

應該給名字first nameinput 元素一個 name 屬性。 提示:可以使用 first-name

assert.isNotEmpty(document.querySelector('fieldset:nth-child(1) > label:nth-child(1) > input')?.name);

應該給姓氏last nameinput 元素一個 name 屬性。 提示:可以使用 last-name

assert.isNotEmpty(document.querySelector('fieldset:nth-child(1) > label:nth-child(2) > input')?.name);

應該給 email 添加一個 name 屬性。 提示:可以使用 email

assert.isNotEmpty(document.querySelector('input[type="email"]')?.name);

應該給 password 添加一個 name 屬性。 提示:可以使用 password

assert.isNotEmpty(document.querySelector('input[type="password"]')?.name);

應該給 checkbox 添加一個 name 屬性。 提示:可以使用 terms

assert.isNotEmpty(document.querySelector('input[type="checkbox"]')?.name);

應該給 file 添加一個 name 屬性。 提示:可以使用 file

assert.isNotEmpty(document.querySelector('input[type="file"]')?.name);

應該給 number 添加一個 name 屬性。 提示:可以使用 age

assert.isNotEmpty(document.querySelector('input[type="number"]')?.name);

你應該給 select 元素一個 name 屬性。 提示:可以使用 referrer

assert.isNotEmpty(document.querySelector('select')?.name);

應該給 textarea 元素一個 name 屬性。 提示:可以使用 bio

assert.isNotEmpty(document.querySelector('textarea')?.name);

應該給 option 元素一個 name 屬性。

[...document.querySelectorAll('option')]?.forEach(option => assert.isUndefined(option?.name));

不應該給任何 label 元素一個 name 屬性。

[...document.querySelectorAll('label')]?.forEach(label => assert.isUndefined(label?.name));

不應該給任何 fieldset 元素一個 name 屬性。

[...document.querySelectorAll('fieldset')]?.forEach(fieldset => assert.isEmpty(fieldset?.name));

--seed--

--seed-contents--

<!DOCTYPE html>
<html>
  <head>
    <title>Registration Form</title>
      <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>Registration Form</h1>
    <p>Please fill out this form with the required information</p>
    <form action='https://register-demo.freecodecamp.org'>
--fcc-editable-region--
      <fieldset>
        <label>Enter Your First Name: <input type="text" required /></label>
        <label>Enter Your Last Name: <input type="text" required /></label>
        <label>Enter Your Email: <input type="email" required /></label>
        <label>Create a New Password: <input type="password" pattern="[a-z0-5]{8,}" required /></label>
      </fieldset>
      <fieldset>
        <label><input type="radio" name="account-type" /> Personal Account</label>
        <label><input type="radio" name="account-type" /> Business Account</label>
        <label>
          <input type="checkbox" required /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a>
              </label>
      </fieldset>
      <fieldset>
        <label>Upload a profile picture: <input type="file" /></label>
        <label>Input your age (years): <input type="number" min="13" max="120" />
              </label>
        <label>How did you hear about us?
          <select>
            <option value="">(select one)</option>
            <option value="1">freeCodeCamp News</option>
            <option value="2">freeCodeCamp YouTube Channel</option>
            <option value="3">freeCodeCamp Forum</option>
            <option value="4">Other</option>
          </select>
        </label>
        <label>Provide a bio:
                  <textarea rows="3" cols="30" placeholder="I like coding on the beach..."></textarea>
              </label>
      </fieldset>
--fcc-editable-region--
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>
body {
  width: 100%;
  height: 100vh;
  margin: 0;
  background-color: #1b1b32;
    color: #f5f6f7;
}

label {
    display: block;
    margin: 0.5rem 0;
}