示例工程 22-form-basics

22-form-basics/app/index.html

html · 5.6 KB

运行:表单基础 · form / action / method

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>表单基础 · form / action / method</title>
    <meta name="description" content="GET 与 POST 表单、name/value、提交按钮与目标页。" />
    <style>
      * { box-sizing: border-box; }
      body {
        margin: 0;
        min-height: 100vh;
        padding: 32px 20px 40px;
        background: #f8fafc;
        color: #0f172a;
        font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
        line-height: 1.7;
      }
      .page { width: min(100%, 760px); margin: 0 auto; }
      .site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
      .brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
      nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
      nav a + a { margin-left: 14px; }
      main { padding: 28px 0; }
      .eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
      h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); line-height: 1.15; }
      .lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
      section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
      h2 { margin: 0 0 10px; font-size: 22px; }
      p { margin: 0 0 12px; color: #334155; }
      ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
      li + li { margin-top: 4px; }
      form { margin: 0 0 12px; padding: 16px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
      fieldset { margin: 0 0 12px; padding: 12px; border: 1px solid #e2e8f0; border-radius: 8px; }
      legend { padding: 0 6px; color: #475569; font-size: 13px; }
      label { display: block; margin-bottom: 4px; font-size: 14px; color: #334155; }
      input, textarea { width: 100%; max-width: 420px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; font-size: 14px; background: #fff; }
      textarea { min-height: 88px; resize: vertical; }
      button { margin-top: 10px; padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 14px; cursor: pointer; }
      button[type="reset"] { margin-left: 8px; background: #fff; color: #334155; border-color: #cbd5e1; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
      .note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
      footer { margin-top: 30px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        body { padding: 20px 14px 32px; }
        .site-header { flex-direction: column; }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header class="site-header">
        <span class="brand">FORM NOTES</span>
        <nav aria-label="页内导航">
          <a href="#get">GET</a>
          <a href="#post">POST</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 22</p>
        <h1>表单基础</h1>
        <p class="lead">
          表单把用户输入变成一组「name=value」,再按 <code>method</code> 提交到 <code>action</code> 指向的地址。
        </p>

        <section id="get">
          <h2>1. GET:搜索</h2>
          <p>GET 把参数放在 URL 里,适合搜索、筛选、分页。</p>
          <form action="search.html" method="get">
            <label for="q">搜索关键词</label>
            <input id="q" name="q" type="search" placeholder="例如:表单" />
            <button type="submit">搜索</button>
          </form>
          <p>提交后地址类似 <code>search.html?q=表单</code>,可收藏、可分享。</p>
        </section>

        <section id="post">
          <h2>2. POST:反馈</h2>
          <p>POST 把数据放在请求体里,适合提交、修改、上传。</p>
          <form action="feedback.html" method="post">
            <fieldset>
              <legend>反馈信息</legend>
              <label for="name">称呼</label>
              <input id="name" name="name" type="text" autocomplete="name" />
              <label for="email">邮箱</label>
              <input id="email" name="email" type="email" autocomplete="email" />
              <label for="message">内容</label>
              <textarea id="message" name="message"></textarea>
            </fieldset>
            <button type="submit">发送反馈</button>
            <button type="reset">清空</button>
          </form>
          <p class="note">POST 不会把内容暴露在 URL 里,但也不等于加密;敏感数据必须用 HTTPS。</p>
        </section>

        <section id="rules">
          <h2>3. 规则</h2>
          <ul>
            <li>每个控件都要有 <code>name</code>,否则不会提交。</li>
            <li>提交按钮用 <code>type="submit"</code>。</li>
            <li>搜索、筛选用 GET;提交、修改用 POST。</li>
            <li>表单要有明确的 <code>action</code>,不要依赖默认行为。</li>
            <li>提交按钮不是链接,不要用 <code>a</code> 代替。</li>
            <li>不要用 GET 提交密码或隐私数据。</li>
          </ul>
        </section>
      </main>

      <footer>
        <p>示例表单为虚构,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>