目 录CONTENT

文章目录

HTML表单详解

Administrator
2025-10-15 / 0 评论 / 0 点赞 / 3 阅读 / 0 字 / 正在检测是否收录...

HTML表单标签详解

表单基础结构

HTML表单通<form>标签创建,包含两个核心属性:

<form action="/submit" method="post">

  <!-- 表单项放置区域 -->

</form>
  • action:指定表单数据提交的URL

  • method:定义数据传输方式(GET/POST)


常用表单项标签

1. input标签核心类型

<input>是最灵活的表单控件,通过type属性实现不同功能:

类型(type)

作用

示例代码

text

单行文本输入

<input type="text" placeholder="用户名">

password

密码输入(掩码显示)

<input type="password" name="pwd">

email

邮箱验证输入

<input type="email" required>

number

数字输入(带步进控件)

<input type="number" min="1" max="100">

date

日期选择器

<input type="date">

file

文件上传

<input type="file" accept=".jpg,.png">

checkbox

多选框

<input type="checkbox" id="agree">

radio

单选框

<input type="radio" name="gender" value="male">

submit

提交按钮

<input type="submit" value="提交">

reset

重置表单

<input type="reset">

hidden

隐藏字段

<input type="hidden" name="token" value="abc123">

2. 其他重要表单标签

<!-- 多行文本域 -->
<textarea rows="4" cols="50">默认内容</textarea>

<!-- 下拉选择框 -->
<select name="colors">
  <option value="red">红色</option>
  <option value="blue" selected>蓝色</option>
</select>

<!-- 按钮标签 -->
<button type="button">普通按钮</button>

高级表单特性

  1. 字段分组

    <fieldset>
      <legend>联系信息</legend>
      <!-- 表单控件 -->
    </fieldset>
    
  2. 数据列表

    <input list="browsers">
    <datalist id="browsers">
      <option value="Chrome">
      <option value="Firefox">
    </datalist>
    
  3. 输入验证

    <input type="url" required>
    

最佳实践建议

  1. 始终为<input>添加name属性用于数据识别

  2. 使用<label>关联表单控件提升可访问性:

    <label for="username">用户名:</label>
    <input id="username" type="text">
    
  3. 移动端适配使用<input type="tel">触发数字键盘

  4. 通过autocomplete属性管理浏览器自动填充。

0

评论区