示例工程 26-select-radio-checkbox

26-select-radio-checkbox/app/index.html

html · 6.9 KB

运行:选择控件 · select / radio / checkbox

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>选择控件 · select / radio / checkbox</title>
    <meta name="description" content="单选、多选、下拉与分组的最小示例。" />
    <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; padding: 16px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
      fieldset { margin: 0 0 16px; padding: 12px; border: 1px solid #e2e8f0; border-radius: 8px; }
      legend { padding: 0 6px; color: #475569; font-size: 14px; }
      label { display: block; margin: 6px 0; font-size: 14px; color: #334155; }
      .inline { display: inline-flex; align-items: center; gap: 6px; margin-right: 16px; }
      input[type="radio"], input[type="checkbox"] { width: auto; margin: 0; }
      select { width: 100%; max-width: 420px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; font-size: 14px; background: #fff; }
      select[multiple] { min-height: 120px; }
      button { margin-top: 4px; padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 14px; }
      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="#radio">单选</a>
          <a href="#checkbox">多选</a>
          <a href="#select">下拉</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 26</p>
        <h1>选择控件</h1>
        <p class="lead">
          单选用 radio,多选用 checkbox,选项多用 select。它们都靠 <code>name</code> 提交,靠 <code>label</code> 被理解。
        </p>

        <form action="preview.html" method="post">
          <section id="radio">
            <h2>1. 单选</h2>
            <fieldset>
              <legend>套餐</legend>
              <label class="inline"><input type="radio" name="plan" value="basic" /> 基础</label>
              <label class="inline"><input type="radio" name="plan" value="standard" checked /> 标准</label>
              <label class="inline"><input type="radio" name="plan" value="pro" /> 专业</label>
            </fieldset>
            <p class="note">同一组 radio 使用相同 <code>name</code>,选中一个会自动取消其他。</p>
          </section>

          <section id="checkbox">
            <h2>2. 多选</h2>
            <fieldset>
              <legend>附加功能</legend>
              <label class="inline"><input type="checkbox" name="features" value="backup" /> 自动备份</label>
              <label class="inline"><input type="checkbox" name="features" value="monitor" /> 监控告警</label>
              <label class="inline"><input type="checkbox" name="features" value="logs" /> 日志导出</label>
            </fieldset>
            <p>同一组 checkbox 可以选多个,提交时会出现多个同名值。</p>
          </section>

          <section id="select">
            <h2>3. 下拉</h2>
            <label for="industry">行业</label>
            <select id="industry" name="industry">
              <option value="" disabled selected>请选择行业</option>
              <optgroup label="互联网">
                <option value="saas">SaaS</option>
                <option value="ecommerce">电商</option>
                <option value="content">内容平台</option>
              </optgroup>
              <optgroup label="金融">
                <option value="bank">银行</option>
                <option value="insurance">保险</option>
              </optgroup>
              <optgroup label="其他">
                <option value="education">教育</option>
                <option value="other">其他</option>
              </optgroup>
            </select>

            <label for="channels">通知渠道(可多选)</label>
            <select id="channels" name="channels" multiple size="4">
              <option value="email" selected>邮件</option>
              <option value="sms">短信</option>
              <option value="webhook">Webhook</option>
              <option value="none">不接收</option>
            </select>
            <p class="note">多选下拉在移动端体验一般,选项多时考虑复选框组。</p>
          </section>

          <button type="submit">提交</button>
        </form>

        <section id="rules">
          <h2>4. 规则</h2>
          <ul>
            <li>单选组同名,每个选项有唯一 id 与 label。</li>
            <li>多选组同名,值是具体选项,不是「开/关」。</li>
            <li>组用 <code>fieldset</code> / <code>legend</code> 命名。</li>
            <li>下拉第一项占位用 <code>disabled selected</code>。</li>
            <li>选项多时分 <code>optgroup</code>,不要一长串。</li>
            <li>不要用 <code>div</code> 模拟单选与复选框。</li>
          </ul>
        </section>
      </main>

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