HTML 表单示例
本章节提供多种实用的表单示例,涵盖用户注册、登录、搜索、联系表单等常见场景。
登录表单
最基本的登录表单,包含用户名和密码输入。
<form action="/login" method="POST">
<h2>用户登录</h2>
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</div>
<div>
<label>
<input type="checkbox" name="remember"> 记住我
</label>
</div>
<button type="submit">登录</button>
</form> 特点
- 使用
type="password"隐藏密码字符 required属性确保必填- 复选框提供"记住我"功能
注册表单
完整的用户注册表单,包含多种输入类型和验证。
<form action="/register" method="POST">
<fieldset>
<legend>基本信息</legend>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" minlength="3" maxlength="20" required>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" minlength="8" required>
<label for="confirm-password">确认密码:</label>
<input type="password" id="confirm-password" name="confirm-password" required>
</fieldset>
<fieldset>
<legend>详细信息</legend>
<label for="phone">手机号:</label>
<input type="tel" id="phone" name="phone" pattern="1[3-9][0-9]{9}">
<label for="birthday">生日:</label>
<input type="date" id="birthday" name="birthday">
<label for="gender">性别:</label>
<select id="gender" name="gender">
<option value="">请选择</option>
<option value="male">男</option>
<option value="female">女</option>
<option value="other">其他</option>
</select>
</fieldset>
<div>
<label>
<input type="checkbox" name="terms" required>
我已阅读并同意<a href="/terms">服务条款</a>
</label>
</div>
<button type="submit">注册</button>
</form> 使用的元素
<fieldset>和<legend>对表单进行分组<select>创建下拉选择框minlength和maxlength限制字符长度pattern使用正则验证手机号
联系表单
用于用户提交咨询或反馈的联系表单。
<form action="/contact" method="POST">
<h2>联系我们</h2>
<div class="form-row">
<div>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
</div>
<div>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
</div>
</div>
<div>
<label for="subject">主题:</label>
<select id="subject" name="subject" required>
<option value="">请选择咨询类型</option>
<option value="general">一般咨询</option>
<option value="technical">技术支持</option>
<option value="billing">账单问题</option>
<option value="feedback">意见反馈</option>
</select>
</div>
<div>
<label for="message">留言内容:</label>
<textarea id="message" name="message" rows="5" cols="50" placeholder="请输入您的留言内容..." required></textarea>
</div>
<button type="submit">提交</button>
</form> 提示
<textarea>支持多行文本输入rows和cols属性控制文本域大小- 下拉菜单提供预设的咨询类型选项
调查问卷表单
展示复选框和单选按钮的典型用法。
<form action="/survey" method="POST">
<h2>用户满意度调查</h2>
<fieldset>
<legend>您的职业:</legend>
<label>
<input type="radio" name="occupation" value="student" required> 学生
</label>
<label>
<input type="radio" name="occupation" value="developer"> 开发人员
</label>
<label>
<input type="radio" name="occupation" value="designer"> 设计师
</label>
<label>
<input type="radio" name="occupation" value="other"> 其他
</label>
</fieldset>
<fieldset>
<legend>您使用过以下哪些功能?(多选)</legend>
<label>
<input type="checkbox" name="features" value="editor"> 在线编辑器
</label>
<label>
<input type="checkbox" name="features" value="converter"> 格式转换器
</label>
<label>
<input type="checkbox" name="features" value="validator"> 代码检验器
</label>
<label>
<input type="checkbox" name="features" value="tutorial"> 教程文档
</label>
</fieldset>
<fieldset>
<legend>总体满意度评分:</legend>
<label for="rating">请评分(1-10):</label>
<input type="range" id="rating" name="rating" min="1" max="10" value="8">
</fieldset>
<button type="submit">提交调查</button>
<button type="reset">重置</button>
</form> 表单元素说明
- 单选按钮 (radio):同一 name 值只能选一个
- 复选框 (checkbox):同一 name 值可以选多个
- 滑块 (range):适合评分等范围选择
- 重置按钮 (reset):恢复表单到初始状态
文件上传表单
演示文件上传的多种配置方式。
<form action="/upload" method="POST" enctype="multipart/form-data">
<h2>文件上传</h2>
<div>
<label for="avatar">上传头像:</label>
<input type="file" id="avatar" name="avatar" accept="image/png,image/jpeg,image/gif" required>
<p>支持的格式:PNG、JPEG、GIF,最大 2MB</p>
</div>
<div>
<label for="documents">上传文档:</label>
<input type="file" id="documents" name="documents" accept=".pdf,.doc,.docx" multiple>
<p>支持 PDF、Word 文档,可选择多个文件</p>
</div>
<div>
<label for="description">文件描述:</label>
<textarea id="description" name="description" rows="3"></textarea>
</div>
<button type="submit">上传</button>
</form> 重要
- 文件上传表单必须设置
enctype="multipart/form-data" accept属性只是提示,不影响服务器端验证- 服务器端必须再次验证文件类型和大小
搜索框
简单的搜索表单,用于站内搜索功能。
<form action="/search" method="GET" class="search-form">
<input type="search" name="q" placeholder="搜索教程..." autofocus>
<button type="submit">搜索</button>
</form> 提示
- 搜索表单通常使用
GET方法,参数显示在URL中便于分享 autofocus让用户直接开始输入- 可以使用
type="search"获取原生搜索框样式