博客列表 >细说表单控件:按钮、下拉列表、文本域、域分组元素等常用属性与事件

细说表单控件:按钮、下拉列表、文本域、域分组元素等常用属性与事件

溪边小树
溪边小树原创
2020年04月07日 16:55:181684浏览

课前老师推荐了几种技术资料查询的方式,非常实用:
1、w3cschool:https://www.w3school.com.cn/
2、php中文网中的原生手册等
3、mdn:https://developer.mozilla.org/zh-CN/ 全球开发者的乐园,专业人士使用的查询手册

4. 按钮控件元素<button>

4.1 与<input>对比

序号 <button> 替代的<input>
1 <button type="...">按钮文本</button> <input type="..." value="按钮文本">
2 <button><img src="..."></button> <input type="image" src="...">图像域

4.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"

4.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>按钮元素</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>
  • register.phplogin.php源码
  1. <?php
  2. // 查看表单提交的请求参数
  3. print_r($_REQUEST);

5. 下拉列表元素<select>

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

5.1 <select>属性

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

5.2 <optgroup>属性

属性 描述
label 列表分组名称

5.3 <option>属性

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

5.4 <select>事件属性

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

5.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>下拉列表</title>
  7. </head>
  8. <body>
  9. <form action="">
  10. <!-- 当前默认选项值是"CSS3", 点击CSS3不会触发change事件,除此之外都会触发 -->
  11. <!-- click事件不在乎当前值是否发生变化, 只要点击一定触发, 注意与change事件的区别 -->
  12. <select
  13. name="lang"
  14. id="lang"
  15. size="10"
  16. multiple
  17. onchange="alert(this.value)"
  18. onclick="alert(this.value)"
  19. >
  20. <optgroup label="前端">
  21. <option value="html5">HTML5</option>
  22. <option value="css3" selected>CSS3</option>
  23. <option value="javascript" disabled>JavaScript</option>
  24. <!-- 使用label属性,可省略选项文本,并将option转为单标签 -->
  25. <option value="es6" label="ECMScript6"> </option
  26. ><option value="jquery" label="jQuery"> </option
  27. ></optgroup>
  28. <optgroup label="后端">
  29. <option value="php" label="PHP">PHP</option
  30. ><option value="mysql" label="MySQL">MySQL</option
  31. ><option value="javascript" label="Laravel">Laravel</option
  32. ></optgroup>
  33. </select>
  34. </form>
  35. </body>
  36. </html>

6. 多行文本域元素<textarea>

6.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 自动完成(布尔属性)

6.2 事件属性

序号 事件 描述
1 onclick 点击时触发
2 onchange 文本被修改时触发
3 onselect 文本被选中时触发

提示: <textarea>是双标签,没有value属性,标签内部的文本就是参数值

6.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>文本域</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: rgb(226, 43, 67);
  23. cursor: pointer;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <!-- 表单内部为空,控件全部使用form属性与之绑定 -->
  29. <form action="" id="common"></form>
  30. <!-- change:值改变时触发, select:选中文本时触发 -->
  31. <textarea
  32. name="reply"
  33. id=""
  34. cols="30"
  35. rows="10"
  36. minlength="5"
  37. maxlength="50"
  38. form="common"
  39. placeholder="不超过50字符"
  40. onchange="alert('内容改变了')"
  41. onselect="this.style.color='red'"
  42. ></textarea>
  43. <!-- 动态设置处理脚本与请求类型 -->
  44. <button
  45. type="submit"
  46. form="common"
  47. formaction="register.php"
  48. formmethod="POST"
  49. >
  50. 提交
  51. </button>
  52. </body>
  53. </html>

7. 表单域分组元素<fieldset>

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

7.1 常用属性

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

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

7.2 示例

  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>表单域分组元素</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. <!-- 提交设置通过<button>元素完成 -->
  50. <form action="" id="register"></form>
  51. <!-- 表单域分组1 -->
  52. <fieldset name="base" form="register">
  53. <legend>基本信息</legend>
  54. <section>
  55. <input
  56. type="email"
  57. name="email"
  58. placeholder="您的邮箱"
  59. form="register"
  60. autofocus
  61. />
  62. <input
  63. type="password"
  64. name="psw1"
  65. placeholder="您的密码"
  66. form="register"
  67. />
  68. <input
  69. type="password"
  70. name="psw2"
  71. placeholder="重复密码"
  72. form="register"
  73. />
  74. </section>
  75. </fieldset>
  76. <!-- 表单域分组2 -->
  77. <fieldset name="other" form="register">
  78. <legend>选填信息</legend>
  79. <section>
  80. <input
  81. type="text"
  82. name="nickname"
  83. placeholder="您的呢称"
  84. form="register"
  85. />
  86. <input type="number" name="age" min="10" max="70" step="1"
  87. form="register" / placeholder="您的年龄"> <input type="url" name="site"
  88. placeholder="个人站点"" form="register"/>
  89. </section>
  90. </fieldset>
  91. <button
  92. type="submit"
  93. form="register"
  94. formaction="register.php"
  95. formmethod="POST"
  96. formtarget="_blank"
  97. >
  98. 提交
  99. </button>
  100. </body>
  101. </html>

课程学习小结:

在上一次表单基本元素学习之后,进一步深入学习以下表单控件的具体使用,主要包括按钮、下拉列表、文本域、域分组元素等常用属性与事件,这些都是必备的基础知识,必须认真掌握好细节,对后续知识的理解有帮助。
几点注意事项:
1、按钮不用input了,现在用buttom来写;
2、递交动态数据,需要切换到PHPSTUDY服务器,localhost(插件live server)没法调试了,它只能做静态网页的调试;
3、Input中的主要type类型:text文本框、password密码、radio单选框、checkbox复选框、file文件等,特别注意label里的for属性与input里的id属性关联绑定;
4、由于使用不同脚本处理递交登录与注册,form中的属性action=”register.php” method=”post”可以去掉;
5、老师在表单域分组等示例中用了一些CSS代码,还不太能看得懂,明天课程会学到。

上一条:css选择器下一条:0406CSS 选择器
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议