当前位置: 首页 > news >正文

HTML 表单学习笔记

一、表单概述

  • 核心作用:收集用户输入数据并提交到服务器

  • 组成元素:输入框、按钮、选择框等交互控件

  • 数据流向:用户填写 → 客户端 → 服务器端处理


二、<form>标签属性

属性作用示例
action指定数据提交的服务器地址<form action="/submit.php">
method设置HTTP请求方法(GET/POST)<form method="POST">
enctype数据编码方式(默认urlencoded,文件上传用multipart/form-data)<form enctype="multipart/form-data">
name表单名称,用于JS操作<form name="loginForm">
id唯一标识符,用于CSS/JS操作<form id="regForm">

注意事项

  • GET方法将数据附加到URL(可见,有长度限制)

  • POST方法在请求体中发送数据(更安全,适合敏感信息)


三、表单控件

1. 输入类控件(<input>)

类型用途示例
text单行文本输入<input type="text" name="username">
password密码输入(内容掩码)<input type="password" name="pwd">
email邮箱地址(自动格式验证)<input type="email" name="email">
number数字输入(可设min/max)<input type="number" min="0" max="100">
date日期选择<input type="date" name="birthday">
checkbox多选框(可多选)<input type="checkbox" name="hobby" value="music">音乐
radio单选按钮(name需相同)<input type="radio" name="gender" value="male">男
file文件上传<input type="file" name="avatar">
submit提交按钮<input type="submit" value="提交">
reset重置表单<input type="reset" value="清空">

2. 其他控件

  • <textarea>:多行文本输入

    <textarea name="bio" rows="4" cols="50"></textarea>
  • <select>:下拉选择框

    <select name="city"><option value="bj">北京</option><option value="sh">上海</option>
    </select>
  • <button>:多功能按钮

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

四、表单验证(HTML5)

属性作用示例
required必填字段<input type="text" required>
pattern正则表达式验证<input pattern="\d{6}">
min/max数值/日期范围限制<input type="number" min="1">
maxlength最大字符数限制<input maxlength="10">
minlength最小字符数限制<input minlength="3">

五、布局与优化

1. 标签关联

<label for="username">用户名:</label>
<input type="text" id="username">
  • 提升可访问性

  • 点击标签可聚焦输入框

2. 分组显示

<fieldset><legend>联系信息</legend><!-- 表单控件 -->
</fieldset>

3. 样式建议

  • 使用CSS Grid/Flex布局对齐表单元素

  • 添加:focus样式提升交互体验

  • 错误提示使用醒目颜色(如红色)


六、注意事项

  1. 敏感数据必须使用POST方法

  2. 文件上传必须设置enctype="multipart/form-data"

  3. 客户端验证不能替代服务器端验证

  4. 单选框需要相同name属性实现互斥

  5. 推荐使用<button>替代<input type="submit">(样式更灵活)


七、综合示例

<form action="/register" method="POST" enctype="multipart/form-data"><fieldset><legend>用户注册</legend><label for="email">邮箱:</label><input type="email" id="email" name="email" required><label for="password">密码:</label><input type="password" id="password" name="pwd" minlength="6" required><label>性别:<input type="radio" name="gender" value="male" required> 男<input type="radio" name="gender" value="female"> 女</label><label>上传头像:<input type="file" name="avatar"></label><button type="submit">注册</button><button type="reset">重置</button></fieldset>
</form>

相关文章:

  • 19_大模型微调和训练之-基于LLamaFactory+LoRA微调LLama3
  • 第42讲:走进智慧农业的“感知神经系统”——农田遥感 + 边缘计算的融合实践
  • [SpringBoot]配置文件
  • Kubeflow 快速入门实战(三) - Qwen2.5 微调全流程
  • 大模型学习笔记 day01
  • SVM(支持向量机)
  • app控制的吸尘拖地扫地机研究
  • 【上位机——MFC】菜单类与工具栏
  • 483. 找到字符串中所有的字母异位词
  • 深度图可视化
  • opencv函数展示4
  • 《TCP/IP详解 卷1:协议》之第三章:IP:网际协议
  • 数码管静态显示一位字符(STC89C52单片机)
  • 【架构】-- StarRocks 和 Doris 介绍与选型建议
  • 使用open3d将pcd点云按照颜色等级分块显示并令其随颜色变化播放
  • 【EasyPan】项目常见问题解答(自用持续更新中…)
  • 论文笔记-arXiv2025-FilterLLM
  • 【论文阅读23】-地下水预测-TCN-LSTM-Attention(2024-11)
  • 24.中医知识问答删除历史对话功能前端代码实现
  • 基于Redis实现高并发抢券系统的数据同步方案详解
  • 在因关税战爆火的敦煌网上,美国人爱买什么中国商品
  • 美国土安全部长餐厅遇窃,重要证件被盗走
  • 动力电池、风光电设备退役潮来袭,国家队即将推出“再生计划”
  • 沉浸式表演+窥探式观演,《情人》三登人艺实验剧场
  • 豫章故郡,剑指演艺经济新高地
  • 北京媒体锐评男子地铁辱骂他人:北京地铁永远欢迎沾着泥巴的普通劳动者