博客列表 >表单与控件元素(按钮常用属性、下拉列表常用属性、文件域常用属性、表单域分组元素常用属性)0404

表单与控件元素(按钮常用属性、下拉列表常用属性、文件域常用属性、表单域分组元素常用属性)0404

ALWAYS
ALWAYS原创
2020年04月05日 09:41:20781浏览

1.按钮元素<button>

1.1 与<input>对比

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

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

1.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. button {
  20. height: 30px;
  21. border: none;
  22. outline: none;
  23. }
  24. form > section {
  25. display: grid;
  26. grid-template-columns: 60px 1fr;
  27. }
  28. h3 {
  29. text-align: center;
  30. }
  31. section:last-of-type {
  32. display: grid;
  33. grid-template-columns: 1fr 1fr;
  34. column-gap: 10px;
  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 aotufocus />
  49. </section>
  50. <section>
  51. <label for="password">密码:</label>
  52. <input type="password" name="password" id="password" required />
  53. </section>
  54. <section>
  55. <button
  56. type="sunbmit"
  57. formaction="login.php"
  58. formmethod="POST"
  59. formtarget="_blank"
  60. >
  61. 登录
  62. </button>
  63. <button
  64. type="sunbmit"
  65. formaction="register.php"
  66. formmethod="GET"
  67. formtarget="_blank"
  68. >
  69. 注册
  70. </button>
  71. </section>
  72. </form>
  73. </body>
  74. </html>

2. 下拉列表元素<select>

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

2.1 <select>属性

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

2.2 <optgroup>属性

属性 描述
label 列表分组名称

2.3 <option>属性

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

2.4 <select>事件属性

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

2.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. <select
  11. name="lang"
  12. id="lang"
  13. multiple
  14. size="8"
  15. onchange="alert(this.value)"
  16. onclick="alert(this.value)"
  17. >
  18. <optgroup label="前端">
  19. <option value="HTML5">HTML5</option>
  20. <option value="CSS3">CSS3</option>
  21. <option value="JavaScript">JavaScript</option>
  22. <option value="ECMScript">ECMScript</option>
  23. <option value="JQuery">JQuery</option>
  24. </optgroup>
  25. <optgroup label="后端">
  26. <option value="PHP">PHP</option>
  27. <option value="MYSQL">MYSQL</option>
  28. <option value="Laravel">Laravel</option>
  29. </optgroup>
  30. </select>
  31. </form>
  32. </body>
  33. </html>

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

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

3.2 事件属性

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

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

3.3 示例

```html
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>文本域</title>
<style>
body {
width: 80%;
margin: auto;
display: grid;
row-gap: 15px;
}

  1. button {
  2. height: 30px;
  3. border: none;
  4. outline: none;
  5. background-color: lightseagreen;
  6. color: white;
  7. }
  8. button:hover {
  9. background-color: blueviolet;
  10. cursor: pointer;
  11. }
  12. </style>

</head>

<body>
<!-- 表单内部为空,控件全部使用form属性与之绑定 -->
<form action="" id="common"></form>
<!-- change:值改变时触发, select:选中文本时触发 -->
<textarea
name=”reply”
id=””
cols=”30”
rows=”10”
minlength=”5”
maxlength=”50”
form=”common”
placeholder=”不超过50字符”
onchange=”alert(‘内容改变了’)”
onselect=”this.style.color=’red’”

  1. ></textarea>
  2. <!-- 动态设置处理脚本与请求类型 -->
  3. <button
  4. type="submit"
  5. form="common"
  6. formaction="register.php"
  7. formmethod="POST"
  8. >
  9. 提交
  10. </button>

</body>
</html>

  1. ![](https://img.php.cn/upload/image/977/455/454/1586048118128344.png)
  2. ## 4. 表单域分组元素`<fieldset>`
  3. - 当表单字段非常多时,分组管理很有必要,例如将必填项与选填项分开
  4. - 它只有一个子元素`<legend>`,设置分组标题
  5. ### 4.1 常用属性
  6. | 序号 | 属性 | 描述 |
  7. | ---- | ---------- | ----------------------------- |
  8. | 1 | `name` | 分组名称 |
  9. | 2 | `form` | 分组所属表单,默认是最近的表单 |
  10. | 3 | `disabled` | 禁用分组(布尔属性) |
  11. > `name`,`form`属性仅供参考,提交参数仍依赖内部控件中的`form`属性
  12. ### 4.2 示例
  13. ```html
  14. <!DOCTYPE html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="UTF-8" />
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  19. <title>fieldset 表单域分组元素</title>
  20. <style>
  21. body {
  22. display: grid;
  23. row-gap: 15px;
  24. }
  25. fieldset {
  26. color: lightseagreen;
  27. border-radius: 6px;
  28. border: 2px solid lightseagreen;
  29. }
  30. fieldset:hover {
  31. background-color: lightcyan;
  32. }
  33. fieldset > section {
  34. display: grid;
  35. grid-template-columns: repeat(3, 1fr);
  36. column-gap: 15px;
  37. }
  38. fieldset > legend {
  39. text-align: center;
  40. }
  41. input {
  42. border: none;
  43. outline: none;
  44. border-bottom: 1px solid #666;
  45. background-color: transparent;
  46. }
  47. button {
  48. height: 30px;
  49. border: none;
  50. outline: none;
  51. border-radius: 6px;
  52. background-color: lightseagreen;
  53. color: white;
  54. }
  55. button:hover {
  56. background-color: darkorchid;
  57. cursor: pointer;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <form action="" id="register"></form>
  63. <fieldset form="register">
  64. <legend>基础信息</legend>
  65. <section>
  66. <input
  67. type="email"
  68. name="email"
  69. placeholder="您的邮箱"
  70. from="register"
  71. />
  72. <input
  73. type="password"
  74. name="password1"
  75. placeholder="您的密码"
  76. form="register"
  77. />
  78. <input
  79. type="password"
  80. name="password2"
  81. placeholder="重复密码"
  82. form="register"
  83. />
  84. </section>
  85. </fieldset>
  86. <fieldset form="register">
  87. <legend>选填信息</legend>
  88. <section>
  89. <input
  90. type="text"
  91. name="nickname"
  92. placeholder="您的昵称"
  93. form="register"
  94. />
  95. <input
  96. type="number"
  97. name="age"
  98. placeholder="您到年龄"
  99. min="5"
  100. max="60"
  101. step="5"
  102. form="register"
  103. />
  104. <input type="url" name="site" placeholder="个人站点" form="register" />
  105. </section>
  106. </fieldset>
  107. <button
  108. type="submit"
  109. form="register"
  110. formmethod="POST"
  111. formaction="register.php"
  112. formtarget="_blank"
  113. >
  114. 提交
  115. </button>
  116. </body>
  117. </html>

总结:
1.提交按钮尽量使用button按钮。与input区别在于,button的文本写在标签间,input是单标签,文本使用value值显示。
2.form属性中的元素都可以写在form标签外面,只要元素中的form属性与from标签中的ID值一致即可。
3.下拉列表中有两个实践,onclick 为点击时触发,onchange是焦点改变时触发。
4.当formmethod=”POST” formtarget=”_blank”时,打开新的网页,当formmethod=”get”,下在register.php文件。

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