示例工程 49-project-content

49-project-content/demo-02-form/index.html

html · 3.6 KB

运行:表单与错误摘要演示

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>表单与错误摘要演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
      .page { max-width: 720px; margin: 0 auto; }
      h1 { margin: 0 0 12px; font-size: 28px; }
      .summary { margin: 0 0 16px; padding: 12px 14px; border-left: 4px solid #dc2626; border-radius: 6px; background: #fef2f2; color: #991b1b; font-size: 14px; }
      .summary h2 { margin: 0 0 4px; font-size: 15px; }
      .summary ul { margin: 0; padding-left: 20px; }
      form { padding: 16px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      fieldset { margin: 0 0 14px; padding: 10px 14px 14px; border: 1px solid #e2e8f0; border-radius: 10px; }
      legend { padding: 0 4px; font-weight: 700; font-size: 14px; }
      .field { margin-bottom: 12px; }
      label { display: block; margin-bottom: 3px; font-size: 14px; font-weight: 600; }
      .hint { margin: 0 0 4px; color: #64748b; font-size: 13px; }
      input, textarea, select { width: 100%; max-width: 420px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; }
      textarea { min-height: 90px; }
      [aria-invalid="true"] { border-color: #dc2626; }
      .error { margin: 4px 0 0; color: #b91c1c; font-size: 13px; }
      .consent { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; font-weight: 400; }
      button { padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
    </style>
  </head>
  <body>
    <div class="page">
      <h1>报修表单</h1>

      <div class="summary" role="alert" aria-labelledby="summary-title">
        <h2 id="summary-title">提交失败,请修正 2 处</h2>
        <ul>
          <li><a href="#phone">电话格式不正确</a></li>
          <li><a href="#detail">问题描述太短</a></li>
        </ul>
      </div>

      <form action="#report" method="post">
        <fieldset>
          <legend>联系方式</legend>
          <div class="field">
            <label for="name">称呼</label>
            <input id="name" name="name" type="text" autocomplete="name" required />
          </div>
          <div class="field">
            <label for="phone">电话</label>
            <p class="hint" id="phone-hint">格式如 13800138000。</p>
            <input id="phone" name="phone" type="tel" inputmode="tel" autocomplete="tel" value="1380013" aria-invalid="true" aria-describedby="phone-hint phone-error" />
            <p class="error" id="phone-error">请输入 11 位手机号,例如 13800138000。</p>
          </div>
        </fieldset>

        <fieldset>
          <legend>问题信息</legend>
          <div class="field">
            <label for="detail">问题描述</label>
            <p class="hint" id="detail-hint">至少 10 个字,写清现象与时间。</p>
            <textarea id="detail" name="detail" minlength="10" aria-invalid="true" aria-describedby="detail-hint detail-error">漏水</textarea>
            <p class="error" id="detail-error">请至少写 10 个字,说明现象、开始时间与已尝试的处理。</p>
          </div>
        </fieldset>

        <label class="consent">
          <input type="checkbox" name="consent" value="yes" required />
          我同意物业使用以上信息处理报修。
        </label>

        <p><button type="submit">重新提交</button></p>
      </form>
    </div>
  </body>
</html>