HTML——表单标签

表单

作用:收集用户信息

使用场景:登录页面、注册页面、搜索区域。

一、input 标签

input 标签 type 属性值不同,则功能不同。

A、type 属性

type属性值 说明
text 文本框,用于输入单行文本
password 密码框
radio 单选框
checkbox 多选框
file 上传文件

1、text 属性

html 复制代码
文本框:<input type="text">

2、password 属性

html 复制代码
密码框:<input type="password">

3、radio 属性

html 复制代码
    性别:男<input type="radio">

单选框常用属性:

属性名 作用 特殊说明
name 控件名称 控件分组,同组只能选中一个(单选功能)
checked 默认选中 属性名和属性值相同,简写为一个的单词

a、name 属性

html 复制代码
 性别:
 男<input type="radio" name="gender">   
 女<input type="radio" name="gender">
 <!-- name值可以自定义,但是同一组的name值必须相同 -->

b、checked 属性

html 复制代码
性别:
男<input type="radio" name="gender"> 
女<input type="radio" name="gender" checked>
<!-- 使用checked属性,该选项默认选中 -->

4、checkbox 属性

html 复制代码
苹果<input type="checkbox">
香蕉<input type="checkbox">

checked 默认选中属性

html 复制代码
苹果<input type="checkbox" checked>
香蕉<input type="checkbox" checked>
石榴<input type="checkbox">

5、file 属性

(默认只能上传一个文件)

html 复制代码
上传文件<input type="file">

点击选择文件后,选择所需要上传的文件

-multiple 属性(实现多个文件上传)

html 复制代码
上传文件:<input type="file" multiple>

B、input标签占位文本

占位文本:提示信息

文本框和密码框都可以使用

html 复制代码
<input type="" placeholder="提示信息">

占位文本默认灰色,输入信息后占位文本消失,输入信息显示效果灰色

二、下拉菜单

标签:select 嵌套 option,select 是下拉菜单整体,option 是下拉菜单的每一项

html 复制代码
<select>
    <option>北京</option> 
    <option>上海</option>
    <option>杭州</option>
    <option>深圳</option>
</select>

可以通过 selected 属性,设置默认选项

html 复制代码
<select>
    <option>北京</option> 
    <option>上海</option>
    <option selected>杭州</option>
    <option>深圳</option>
</select>

三、文本域

作用:多行输入文本的表单控件

标签:textarea,双标签

html 复制代码
<textarea >请输入内容</textarea>

四、label 标签

作用:在网页中,某个标签的说明文本。

用 label 标签绑定文字和表单控件的关系,增大表单控件的点击范围。

A、写法一

label 标签只包裹内容,不包裹表单控件。

设置 label 标签的 for 属性值和表单控件的 id 属性值相同。

html 复制代码
性别:
<input type="radio" name="gender" id="man"><label for="man">男</label>
<input type="radio" name="gender" id="woman"><label for="woman">女</label>

普通的单选框只能通过点击"选项框(圆圈)"进行勾选,而 label 标签可以通过点击"选项框"和"label 标签中的文字"进行勾选。

B、写法二

使用 label 标签包裹文字和控制表单,不需要属性。

html 复制代码
<label><input type="radio">女</label>

提示: 文本框、密码框、上传文件、单选框、多选框、下拉菜单、文本域等均可使用 label 标签。

五、button 标签

type 属性

type 属性值 说明
submit 提交按钮,点击后可以提交数据到后台(省略type属性依旧是提交功能)
reset 重置按钮,点击后将表单控件恢复到默认值
button 普通按钮,默认没有功能,一般配合JavaScript使用

:实现功能需要将代码写在form表单区域

(action:发送数据的地址)

html 复制代码
<form action="">
    用户名:<input type="text">
    <br>
    密码:<input type="password">
    <br>
    <button type="submit">提交</button>
    <button type="reset">重置</button>
    <button type="button">按钮</button>
</form>
相关推荐
一个网络学徒4 小时前
python5
java·服务器·前端
tiantian_cool4 小时前
Claude Opus 4.6 模型新特性(2026年2月5日发布)
前端
0思必得04 小时前
[Web自动化] Selenium获取元素的子元素
前端·爬虫·selenium·自动化·web自动化
用户5757303346244 小时前
🌟 从一行 HTML 到屏幕像素:浏览器是如何“画”出网页的?
前端
NEXT064 小时前
React Hooks 进阶:useState与useEffect的深度理解
前端·javascript·react.js
sure2824 小时前
React Native应用中使用sqlite数据库以及音乐应用中的实际应用
前端·react native
CHU7290354 小时前
扭蛋机盲盒小程序前端功能设计解析:打造趣味与惊喜并存的消费体验
前端·小程序
前端布道师4 小时前
Web响应式:列表自适应布局
前端
ZeroTaboo4 小时前
rmx:给 Windows 换一个能用的删除
前端·后端
李剑一4 小时前
Vue实现大屏获取当前所处城市及当地天气(纯免费)
前端