表单与控件元素
1.表单控件元素
1.1 常用属性
| 序号 |
属性 |
描述 |
| 1 |
type |
控件类型,如文本框, 复选框… |
| 2 |
name |
请求参数的名称,对应于脚本处理的变量名 |
| 3 |
value |
请求参数的值,对应于脚本处理的变量值,使用预定义值控件无效 |
| 4 |
form |
控件所属表单 |
| 5 |
placeholder |
仅限输入框text和password,输入框的提示信息 |
| 6 |
list |
仅限输入框text和password,下拉框智能提示 |
| 7 |
autocomplate |
仅限输入框text和password,自动完成(历史记录) |
| 8 |
maxlength |
仅限输入框text/password, 允许输入最大字符数量 |
| 9 |
checked |
仅限单选框radio, 复选框checkbox(布尔属性) |
| 10 |
readonly |
元素只读,但通过 JavaScript 可修改(布尔属性) |
| 11 |
disabled |
元素禁用,(布尔属性) |
| 12 |
autofocus |
自动获取焦点,一个表单仅限一个控件 |
| 13 |
src |
仅限图像域images, 图像 URL 地址 |
| 14 |
width |
仅限图像域images, 图像宽度 |
| 15 |
height |
仅限图像域images, 图像高度 |
1.2type类型
| 序号 |
类型 |
描述 |
| 1 |
<input type="text"> |
单行文本框 (默认值) |
| 2 |
<input type="password"> |
密码输入框 |
| 3 |
<input type="radio"> |
单选框 |
| 4 |
<input type="checkbox"> |
复选框 |
| 5 |
<input type="image"> |
图像域/提交表单 |
| 6 |
<input type="file"> |
文件上传域 |
| 7 |
<input type="hidden"> |
隐藏域 |
| 序号 |
类型 |
描述 |
| 1 |
<input type="email"> |
电子邮件 |
| 2 |
<input type="data"> |
日期 |
| 2 |
<input type="data"> |
日期 |
| 4 |
<input type="datetime-local"> |
本地日期时间 |
| 5 |
<input type="tel"> |
电话号码 |
| 6 |
<input type="url"> |
URL 地址 |
| 7 |
<input type="number"> |
数值 |
| 8 |
<input type="range"> |
范围拖动条 |
| 9 |
<input type="search"> |
搜索框/移动 |
| 10 |
<input type="color"> |
拾色器 |
1.3 常用事件属性
| 序号 |
事件属性 |
描述 |
| 1 |
onfocus |
获取焦点时触发 |
| 2 |
onblur |
失去焦点时触发 |
| 3 |
onchange |
失去焦点,且值发生变化时触发 |
| 4 |
oninput |
值发生变化(不等失去焦点)时触发 |
| 5 |
onkeydown |
按下键盘时触发 |
| 6 |
onkeyup |
抬起键盘时触发 |
| 7 |
onclick |
鼠标单击时触发 |
| 8 |
onselect |
选择内容文本时触发 |
1.4 示例

1.5 示例代码
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>html基础input</title> </head> <style> form { padding: 20px; width: 350px; box-shadow: 0 0 8px #888; border-radius: 10px; box-sizing: border-box; margin: auto; background-color: lightskyblue; display: grid; gap: 15px; } form > section { display: grid; grid-template-columns: 60px 1fr; } h3 { text-align: center; } section:last-of-type { display: grid; grid-template-columns: 1fr 1fr; column-gap: 10px; } button { height: 30px; border: none; outline: none; } button:hover { background-color: lightseagreen; color: white; cursor: pointer; } </style> <body> <h3>用户注册</h3> <form action="register.php" method="post" enctype="application/x-www-form-urlencoded" id="register" > <section> <label for="username"> 用户名: </label> <input type="text" id="username" placeholder="最好是6位以上16位一下" maxlength="16" required autofocus /> </section> <section> <label for="password"> 密码: </label> <input type="password" id="password" name="password" placeholder="密码要求8位以上16位一下" size="16" required /> </section> <section> <label for="secret">性别:</label> <div class="box"> <input type="radio" name="gender" id="male" value="male" /><label for="male" >男</label > <input type="radio" name="gender" id="female" value="female" /><label for="female" >女</label > <input type="radio" name="gender" id="secret" value="secret" /><label for="secret" checked >保密</label > </div> </section> <section> <label for="programme">兴趣:</label> <div class="box"> <input type="checkbox" name="hobby[]" name="game" id="game" /><label for="" >游戏</label > <input type="checkbox" name="hobby[]" name="tour" id="tour" /><label for="" >旅游</label > <input type="checkbox" name="hobby[]" name="shoot" id="shoot" /><label for="" >摄影</label > <input type="checkbox" name="hobby[]" name="programme" id="programme" /><label for="">编程</label> </div> </section> <!-- 文件域 --> <section> <label for="userpic">头像:</label> <input type="file" name="user_pic" id="userpic" /> <input type="hidden" name="MAX_FILE_SIZE" value="8388608" /> </section> <section> <label for="course">课程:</label> <input type="text" name="course" list="course" /> <datalist id="course"> <option value="HTML/CSS"> </option> <option value="JavaScript"> </option> <option value="PHP"> </option> <option value="Laravel"> </option> </datalist> </section> <!-- 图像域: 提交按钮为图像 --> <input type="image" src="anniu.png" alt="submit" name="submit" width="100" /> </form> <hr /> <!-- 表单控件元素不一定必须写到<form>标签内 --> <!-- 表单控件使用form属性,将它与所属表单绑定 --> <section> <label for="email">邮箱:</label> <input type="email" name="email" id="email" form="register" /> </section> <section> <label for="age">年龄:</label> <input type="number" name="age" id="age" form="register" min="18" max="60" step="2" value="22" /> </section> </body></html>
2.html 基础按钮元素
| 序号 |
<button> |
替代的<input> |
| 1 |
<button type="...">按钮文本</button> |
<input type="..." value="按钮文本"> |
| 2 |
<button><img src="..."></button> |
<input type="image" src="...">图像域 |
2.2 常用属性
| 序号 |
属性 |
描述 |
| 1 |
type |
必须使用预定义的submit, button, reset之一 |
| 2 |
name |
按钮的唯一名称,与 ID 等效 |
| 3 |
value |
按钮文本初始值,可通过 JavaScript 修改 |
| 4 |
disabled |
禁用按钮 |
| 5 |
form |
按钮所属表单(此时按钮type默认类型为submit提交) |
| 6 |
formaction |
设置不同按钮可将表单数据提交到不同的 URL 处理 |
| 7 |
form*** |
动态设置<form>属性值,如formmethod="GET" |
2.3 示例

2.4 示例代码
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>html基础按钮元素</title> <style> form { padding: 20px; width: 350px; box-shadow: 0 0 8px #888; border-radius: 10px; box-sizing: border-box; margin: auto; background-color: lightskyblue; display: grid; gap: 15px; } form > section { display: grid; grid-template-columns: 60px 1fr; } h3 { text-align: center; } section:last-of-type { display: grid; grid-template-columns: 1fr 1fr; column-gap: 10px; } button { height: 30px; border: none; outline: none; } button:hover { background-color: lightseagreen; color: white; cursor: pointer; } </style> </head> <body> <h3>登录/注册</h3> <form action="register.php" method="post"> <section> <label for="email">邮箱:</label> <input type="email" name="email" id="email" required autofocus /> </section> <section> <label for="password">密码:</label> <input type="password" name="password" id="password" required /> </section> <section> <!-- 登录提交到登录页,注册提交到注册页 --> <button type="submit" formaction="login.php" formmethod="POST" formtarget="_blank" > 登录 </button> <button type="submit" formaction="register.php" formmethod="GET" formtarget="_blank" > 注册 </button> </section> </form> </body></html>
3. html 基础下拉列表元素
- 下拉列表使用
<select> + <optgroup> + <option>组合元素实现 - 参数名
name定义在<select>中,参数值value,定义在<option>中
3.1<select>属性
| 序号 |
属性 |
描述 |
| 1 |
name |
请求参数名称/变量名 |
| 2 |
multiple |
是否允许多选(布尔属性) |
| 3 |
size |
允许同时显示的列表项 |
| 3 |
disabled |
是否禁用(布尔属性) |
3.2<optgroup>属性
3.3<option>属性
| 序号 |
属性 |
描述 |
| 1 |
value |
请求参数的值 |
| 2 |
label |
默认选项文本值 |
| 3 |
selected |
是否选中(布尔属性) |
| 3 |
disabled |
是否禁用(布尔属性) |
3.4<select>事件属性
| 序号 |
事件属性 |
描述 |
| 1 |
onchange |
当下拉列表选项值发生变化时才会触发 |
| 2 |
onclick |
只要点击就会触发(选项值可以不改变) |
3.5 示例

3.5 示例代码
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>html基础下拉列表</title> </head> <body> <form action=""> <select name="lang" id="lang" size="8" multiple onchange="alert(this.value)" onclick="alert(this.value)" > <optgroup label="前端"> <option value="html5">HTML5</option> <option value="css3" selected>CSS3</option> <option value="javascript" disabled>JavaScript</option> <option value="es5" label="ECMScript5"> </option ><option value="jquery" label="jQuery"> </option ></optgroup> <optgroup label="后端"> <option value="php" label="PHP"> </option ><option value="mysql" label="MySQL"> </option ><option value="javascript" label="Laravel"> </option ></optgroup> </select> </form> </body></html>
4.多行文本域元素
4.1 常用属性
| 序号 |
属性 |
描述 |
| 1 |
cols |
文本域可视宽度 |
| 2 |
rows |
文本域可输入的行数 |
| 3 |
name |
文本域参数名称 |
| 4 |
form |
绑定所属表单元素 |
| 5 |
minlength |
允许输入最小字符长度 |
| 6 |
maxlength |
允许输入最大字符长度 |
| 7 |
maxlength |
允许输入最大字符长度 |
| 8 |
placeholder |
提示信息占位符 |
| 9 |
wrap |
换行方式:hard/soft默认 |
| 10 |
disabled |
禁用(布尔属性) |
| 11 |
autofocus |
自动获取焦点(布尔属性) |
| 12 |
autocomplete |
自动完成(布尔属性) |
4.2 事件属性
| 序号 |
事件 |
描述 |
| 1 |
onclick |
点击时触发 |
| 2 |
onchange |
文本被修改时触发 |
| 3 |
onselect |
文本被选中时触发 |
提示: <textarea>是双标签,没有value属性,标签内部的文本就是参数值
4.3 示例

4.4 示例代码
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>html基础文本域</title> <style> body { width: 80%; margin: auto; display: grid; row-gap: 15px; } button { height: 30px; border: none; outline: none; background-color: lightseagreen; color: white; } button:hover { background-color: blueviolet; cursor: pointer; } </style> </head> <body> <form action="" id="common"></form> <textarea name="reply" id="" cols="30" rows="10" minlength="3" maxlength="500" form="common" placeholder="最少3个字符但不超过500字符" onchange="alert('内容改变了')" onselect="this.style.color='red'" ></textarea> <!-- 动态设置 --> <button type="submit" form="common" formaction="register.php" formmethod="POST" > 提交 </button> </body></html>
5. 表单域分组元素
- 当表单字段非常多时,分组管理很有必要,例如将必填项与选填项分开
- 它只有一个子元素
<legend>,设置分组标题
5.1 常用属性
| 序号 |
属性 |
描述 |
| 1 |
name |
分组名称 |
| 2 |
form |
分组所属表单,默认是最近的表单 |
| 3 |
disabled |
禁用分组(布尔属性) |
name,form属性仅供参考,提交参数仍依赖内部控件中的form属性
5.2 示例

5.3 示例代码
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>html基础表单域分组元素</title> <style> body { display: grid; row-gap: 15px; } fieldset { color: lightseagreen; border-radius: 6px; border: 2px solid lightseagreen; } fieldset:hover { background-color: lightcyan; } fieldset > section { display: grid; grid-template-columns: repeat(3, 1fr); column-gap: 15px; } fieldset > legend { text-align: center; } input { border: none; outline: none; border-bottom: 1px solid #666; background-color: transparent; } button { height: 30px; border: none; outline: none; border-radius: 6px; background-color: lightseagreen; color: white; } button:hover { background-color: darkorchid; cursor: pointer; } </style> </head> <body> <form action="" id="register"></form> <!-- 表单域分组1 --> <fieldset name="base" form="register"> <legend>基本信息</legend> <section> <input type="email" name="email" placeholder="您的邮箱" form="register" autofocus /> <input type="password" name="psw1" placeholder="您的密码" form="register" /> <input type="password" name="psw2" placeholder="重复密码" form="register" /> </section> </fieldset> <!-- 表单域分组2 --> <fieldset name="other" form="register"> <legend>选填信息</legend> <section> <input type="text" name="nickname" placeholder="您的呢称" form="register" /> <input type="number" name="age" min="18" max="70" step="1" form="register" / placeholder="您的年龄"> <input type="url" name="site" placeholder="个人站点"" form="register"/> </section> </fieldset> <button type="submit" form="register" formaction="register.php" formmethod="POST" formtarget="_blank" > 提交 </button> </body></html>
批改老师:
天蓬老师
批改状态:合格
老师批语:完成的很好, 你可以发挥想象力, 自己试着写一些, 不一定全抄