博客列表 >html基础知识表单与控件元素、基础按钮元素、基础下拉列表元素、多行文本域元素、表单域分组元素

html基础知识表单与控件元素、基础按钮元素、基础下拉列表元素、多行文本域元素、表单域分组元素

῀℡῀ᵒᵐᵍᵎᵎᵎ
῀℡῀ᵒᵐᵍᵎᵎᵎ原创
2020年04月05日 10:50:16715浏览

表单与控件元素


1.表单控件元素

1.1 常用属性

序号 属性 描述
1 type 控件类型,如文本框, 复选框…
2 name 请求参数的名称,对应于脚本处理的变量名
3 value 请求参数的值,对应于脚本处理的变量值,使用预定义值控件无效
4 form 控件所属表单
5 placeholder 仅限输入框textpassword,输入框的提示信息
6 list 仅限输入框textpassword,下拉框智能提示
7 autocomplate 仅限输入框textpassword,自动完成(历史记录)
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"> 隐藏域
  • html5 新增类型
序号 类型 描述
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 示例代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>html基础input</title>
  7. </head>
  8. <style>
  9. form {
  10. padding: 20px;
  11. width: 350px;
  12. box-shadow: 0 0 8px #888;
  13. border-radius: 10px;
  14. box-sizing: border-box;
  15. margin: auto;
  16. background-color: lightskyblue;
  17. display: grid;
  18. gap: 15px;
  19. }
  20. form > section {
  21. display: grid;
  22. grid-template-columns: 60px 1fr;
  23. }
  24. h3 {
  25. text-align: center;
  26. }
  27. section:last-of-type {
  28. display: grid;
  29. grid-template-columns: 1fr 1fr;
  30. column-gap: 10px;
  31. }
  32. button {
  33. height: 30px;
  34. border: none;
  35. outline: none;
  36. }
  37. button:hover {
  38. background-color: lightseagreen;
  39. color: white;
  40. cursor: pointer;
  41. }
  42. </style>
  43. <body>
  44. <h3>用户注册</h3>
  45. <form
  46. action="register.php"
  47. method="post"
  48. enctype="application/x-www-form-urlencoded"
  49. id="register"
  50. >
  51. <section>
  52. <label for="username">
  53. 用户名:
  54. </label>
  55. <input
  56. type="text"
  57. id="username"
  58. placeholder="最好是6位以上16位一下"
  59. maxlength="16"
  60. required
  61. autofocus
  62. />
  63. </section>
  64. <section>
  65. <label for="password">
  66. 密码:
  67. </label>
  68. <input
  69. type="password"
  70. id="password"
  71. name="password"
  72. placeholder="密码要求8位以上16位一下"
  73. size="16"
  74. required
  75. />
  76. </section>
  77. <section>
  78. <label for="secret">性别:</label>
  79. <div class="box">
  80. <input type="radio" name="gender" id="male" value="male" /><label
  81. for="male"
  82. ></label
  83. >
  84. <input type="radio" name="gender" id="female" value="female" /><label
  85. for="female"
  86. ></label
  87. >
  88. <input type="radio" name="gender" id="secret" value="secret" /><label
  89. for="secret"
  90. checked
  91. >保密</label
  92. >
  93. </div>
  94. </section>
  95. <section>
  96. <label for="programme">兴趣:</label>
  97. <div class="box">
  98. <input type="checkbox" name="hobby[]" name="game" id="game" /><label
  99. for=""
  100. >游戏</label
  101. >
  102. <input type="checkbox" name="hobby[]" name="tour" id="tour" /><label
  103. for=""
  104. >旅游</label
  105. >
  106. <input type="checkbox" name="hobby[]" name="shoot" id="shoot" /><label
  107. for=""
  108. >摄影</label
  109. >
  110. <input
  111. type="checkbox"
  112. name="hobby[]"
  113. name="programme"
  114. id="programme"
  115. /><label for="">编程</label>
  116. </div>
  117. </section>
  118. <!-- 文件域 -->
  119. <section>
  120. <label for="userpic">头像:</label>
  121. <input type="file" name="user_pic" id="userpic" />
  122. <input type="hidden" name="MAX_FILE_SIZE" value="8388608" />
  123. </section>
  124. <section>
  125. <label for="course">课程:</label>
  126. <input type="text" name="course" list="course" />
  127. <datalist id="course">
  128. <option value="HTML/CSS"> </option>
  129. <option value="JavaScript"> </option>
  130. <option value="PHP"> </option>
  131. <option value="Laravel"> </option>
  132. </datalist>
  133. </section>
  134. <!-- 图像域: 提交按钮为图像 -->
  135. <input
  136. type="image"
  137. src="anniu.png"
  138. alt="submit"
  139. name="submit"
  140. width="100"
  141. />
  142. </form>
  143. <hr />
  144. <!-- 表单控件元素不一定必须写到<form>标签内 -->
  145. <!-- 表单控件使用form属性,将它与所属表单绑定 -->
  146. <section>
  147. <label for="email">邮箱:</label>
  148. <input type="email" name="email" id="email" form="register" />
  149. </section>
  150. <section>
  151. <label for="age">年龄:</label>
  152. <input
  153. type="number"
  154. name="age"
  155. id="age"
  156. form="register"
  157. min="18"
  158. max="60"
  159. step="2"
  160. value="22"
  161. />
  162. </section>
  163. </body>
  164. </html>

2.html 基础按钮元素

2.1 与<input>对比

序号 <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 示例代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>html基础按钮元素</title>
  7. <style>
  8. form {
  9. padding: 20px;
  10. width: 350px;
  11. box-shadow: 0 0 8px #888;
  12. border-radius: 10px;
  13. box-sizing: border-box;
  14. margin: auto;
  15. background-color: lightskyblue;
  16. display: grid;
  17. gap: 15px;
  18. }
  19. form > section {
  20. display: grid;
  21. grid-template-columns: 60px 1fr;
  22. }
  23. h3 {
  24. text-align: center;
  25. }
  26. section:last-of-type {
  27. display: grid;
  28. grid-template-columns: 1fr 1fr;
  29. column-gap: 10px;
  30. }
  31. button {
  32. height: 30px;
  33. border: none;
  34. outline: none;
  35. }
  36. button:hover {
  37. background-color: lightseagreen;
  38. color: white;
  39. cursor: pointer;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <h3>登录/注册</h3>
  45. <form action="register.php" method="post">
  46. <section>
  47. <label for="email">邮箱:</label>
  48. <input type="email" name="email" id="email" required autofocus />
  49. </section>
  50. <section>
  51. <label for="password">密码:</label>
  52. <input type="password" name="password" id="password" required />
  53. </section>
  54. <section>
  55. <!-- 登录提交到登录页,注册提交到注册页 -->
  56. <button
  57. type="submit"
  58. formaction="login.php"
  59. formmethod="POST"
  60. formtarget="_blank"
  61. >
  62. 登录
  63. </button>
  64. <button
  65. type="submit"
  66. formaction="register.php"
  67. formmethod="GET"
  68. formtarget="_blank"
  69. >
  70. 注册
  71. </button>
  72. </section>
  73. </form>
  74. </body>
  75. </html>

3. html 基础下拉列表元素

  • 下拉列表使用<select> + <optgroup> + <option>组合元素实现
  • 参数名name定义在<select>中,参数值value,定义在<option>

3.1<select>属性

序号 属性 描述
1 name 请求参数名称/变量名
2 multiple 是否允许多选(布尔属性)
3 size 允许同时显示的列表项
3 disabled 是否禁用(布尔属性)

3.2<optgroup>属性

属性 描述
label 列表分组名称

3.3<option>属性

序号 属性 描述
1 value 请求参数的值
2 label 默认选项文本值
3 selected 是否选中(布尔属性)
3 disabled 是否禁用(布尔属性)

3.4<select>事件属性

序号 事件属性 描述
1 onchange 当下拉列表选项值发生变化时才会触发
2 onclick 只要点击就会触发(选项值可以不改变)

3.5 示例

3.5 示例代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>html基础下拉列表</title>
  7. </head>
  8. <body>
  9. <form action="">
  10. <select
  11. name="lang"
  12. id="lang"
  13. size="8"
  14. multiple
  15. onchange="alert(this.value)"
  16. onclick="alert(this.value)"
  17. >
  18. <optgroup label="前端">
  19. <option value="html5">HTML5</option>
  20. <option value="css3" selected>CSS3</option>
  21. <option value="javascript" disabled>JavaScript</option>
  22. <option value="es5" label="ECMScript5"> </option
  23. ><option value="jquery" label="jQuery"> </option
  24. ></optgroup>
  25. <optgroup label="后端">
  26. <option value="php" label="PHP"> </option
  27. ><option value="mysql" label="MySQL"> </option
  28. ><option value="javascript" label="Laravel"> </option
  29. ></optgroup>
  30. </select>
  31. </form>
  32. </body>
  33. </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 示例代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>html基础文本域</title>
  7. <style>
  8. body {
  9. width: 80%;
  10. margin: auto;
  11. display: grid;
  12. row-gap: 15px;
  13. }
  14. button {
  15. height: 30px;
  16. border: none;
  17. outline: none;
  18. background-color: lightseagreen;
  19. color: white;
  20. }
  21. button:hover {
  22. background-color: blueviolet;
  23. cursor: pointer;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <form action="" id="common"></form>
  29. <textarea
  30. name="reply"
  31. id=""
  32. cols="30"
  33. rows="10"
  34. minlength="3"
  35. maxlength="500"
  36. form="common"
  37. placeholder="最少3个字符但不超过500字符"
  38. onchange="alert('内容改变了')"
  39. onselect="this.style.color='red'"
  40. ></textarea>
  41. <!-- 动态设置 -->
  42. <button
  43. type="submit"
  44. form="common"
  45. formaction="register.php"
  46. formmethod="POST"
  47. >
  48. 提交
  49. </button>
  50. </body>
  51. </html>

5. 表单域分组元素

  • 当表单字段非常多时,分组管理很有必要,例如将必填项与选填项分开
  • 它只有一个子元素<legend>,设置分组标题

5.1 常用属性

序号 属性 描述
1 name 分组名称
2 form 分组所属表单,默认是最近的表单
3 disabled 禁用分组(布尔属性)

name,form属性仅供参考,提交参数仍依赖内部控件中的form属性

5.2 示例

5.3 示例代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>html基础表单域分组元素</title>
  7. <style>
  8. body {
  9. display: grid;
  10. row-gap: 15px;
  11. }
  12. fieldset {
  13. color: lightseagreen;
  14. border-radius: 6px;
  15. border: 2px solid lightseagreen;
  16. }
  17. fieldset:hover {
  18. background-color: lightcyan;
  19. }
  20. fieldset > section {
  21. display: grid;
  22. grid-template-columns: repeat(3, 1fr);
  23. column-gap: 15px;
  24. }
  25. fieldset > legend {
  26. text-align: center;
  27. }
  28. input {
  29. border: none;
  30. outline: none;
  31. border-bottom: 1px solid #666;
  32. background-color: transparent;
  33. }
  34. button {
  35. height: 30px;
  36. border: none;
  37. outline: none;
  38. border-radius: 6px;
  39. background-color: lightseagreen;
  40. color: white;
  41. }
  42. button:hover {
  43. background-color: darkorchid;
  44. cursor: pointer;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <form action="" id="register"></form>
  50. <!-- 表单域分组1 -->
  51. <fieldset name="base" form="register">
  52. <legend>基本信息</legend>
  53. <section>
  54. <input
  55. type="email"
  56. name="email"
  57. placeholder="您的邮箱"
  58. form="register"
  59. autofocus
  60. />
  61. <input
  62. type="password"
  63. name="psw1"
  64. placeholder="您的密码"
  65. form="register"
  66. />
  67. <input
  68. type="password"
  69. name="psw2"
  70. placeholder="重复密码"
  71. form="register"
  72. />
  73. </section>
  74. </fieldset>
  75. <!-- 表单域分组2 -->
  76. <fieldset name="other" form="register">
  77. <legend>选填信息</legend>
  78. <section>
  79. <input
  80. type="text"
  81. name="nickname"
  82. placeholder="您的呢称"
  83. form="register"
  84. />
  85. <input type="number" name="age" min="18" max="70" step="1"
  86. form="register" / placeholder="您的年龄"> <input type="url" name="site"
  87. placeholder="个人站点"" form="register"/>
  88. </section>
  89. </fieldset>
  90. <button
  91. type="submit"
  92. form="register"
  93. formaction="register.php"
  94. formmethod="POST"
  95. formtarget="_blank"
  96. >
  97. 提交
  98. </button>
  99. </body>
  100. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议