博客列表 >表单常用元素--Inupt、Datalist、Button、Textarea、Fieldset

表单常用元素--Inupt、Datalist、Button、Textarea、Fieldset

海阔天空
海阔天空原创
2020年04月05日 17:02:03959浏览

表单常用元素练习

效果图

代码如下:

  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>datalist表单元素示例</title>
  7. <style>
  8. form {
  9. padding: 20px;
  10. width: 400px;
  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: 65px 1fr;
  22. }
  23. input[type="image"] {
  24. justify-self: center;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <form
  30. action=""
  31. method="post"
  32. enctype="application/x-www-form-urlencoded"
  33. id="register"
  34. >
  35. <section>
  36. <label for="textx">用户名:</label>
  37. <input type="text" list="fruitsxx" id="textx" />
  38. <datalist id="fruitsxx">
  39. <option>admin</option>
  40. <option>张三</option>
  41. <option>李四</option>
  42. <option>王五</option>
  43. <option>赵六</option>
  44. </datalist>
  45. </section>
  46. <section>
  47. <label for="password">密码:</label>
  48. <input type="password" name="password" id="password" required />
  49. </section>
  50. <section>
  51. <label for="male">性别:</label>
  52. <div>
  53. <!-- 只允许返回一个值,多个input的name属性值必须相同 -->
  54. <input type="radio" name="gender" id="male" value="male" /><label
  55. for="male"
  56. ></label
  57. >
  58. <input
  59. type="radio"
  60. name="gender"
  61. id="female"
  62. value="female"
  63. checked
  64. /><label for="female"></label>
  65. </div>
  66. </section>
  67. <section>
  68. <label for="programme">兴趣:</label>
  69. <div>
  70. <!-- 允许返回多个值,属性名采用数组格式,便于后端处理 -->
  71. <input type="checkbox" name="hobby[]" id="game" checked /><label
  72. for="game"
  73. >电玩</label
  74. >
  75. <input type="checkbox" name="hobby[]" id="basketball" checked /><label
  76. for="basketball"
  77. >篮球</label
  78. >
  79. <input type="checkbox" name="hobby[]" value="swim" id="swim" /><label
  80. for="swim"
  81. >游泳</label
  82. >
  83. <input
  84. type="checkbox"
  85. name="hobby[]"
  86. value="programme"
  87. id="programme"
  88. checked
  89. /><label for="programme">编程</label>
  90. </div>
  91. </section>
  92. <section>
  93. <label for="userpic">头像:</label>
  94. <!-- 设置<form enctype="multipart/form-data">,且为POST提交 才有效-->
  95. <input type="file" name="user_pic" id="userpic" />
  96. <!-- 隐藏域: 限制上传文件大小不超过8M -->
  97. <input type="hidden" name="MAX_FILE_SIZE" value="8388608" />
  98. </section>
  99. <!-- 当前默认选项值是"CSS3", 点击CSS3不会触发change事件,除此之外都会触发 -->
  100. <!-- click事件不在乎当前值是否发生变化, 只要点击一定触发, 注意与change事件的区别 -->
  101. <!-- multiple开启多选功能 -->
  102. <!--onclick="alert('点击了'+this.value)"-->
  103. <section>
  104. <label for="home">籍贯:</label>
  105. <select
  106. name="jiguan"
  107. id="home"
  108. size="4"
  109. multiple
  110. onchange="alert(this.value)"
  111. >
  112. <optgroup label="山东">
  113. <option value="济南">济南</option>
  114. <option value="青岛" selected>青岛</option>
  115. <option value="潍坊">潍坊</option>
  116. <!-- 使用label属性,可省略选项文本,并将option转为单标签 -->
  117. <option value="德州" label="德州"> </option
  118. ><option value="威海" label="威海" disabled> </option
  119. ></optgroup>
  120. <optgroup label="河北">
  121. <option value="石家庄" label="石家庄"> </option
  122. ><option value="衡水" label="衡水"> </option
  123. ><option value="沧州" label="沧州"> </option
  124. ></optgroup>
  125. </select>
  126. </section>
  127. <section>
  128. <label for="jianli">简历:</label>
  129. <!-- change:值改变时触发, select:选中文本时触发 -->
  130. <textarea
  131. name="reply"
  132. id="jianli"
  133. cols="30"
  134. rows="10"
  135. minlength="5"
  136. maxlength="50"
  137. placeholder="不超过50字符"
  138. onchange="alert('内容改变了')"
  139. onselect="this.style.color='red'"
  140. ></textarea>
  141. <!-- 动态设置处理脚本与请求类型 -->
  142. <!-- <button
  143. type="submit"
  144. formaction="register.php"
  145. formmethod="POST"
  146. formtarget="_blank"
  147. >
  148. 提交
  149. </button> -->
  150. </section>
  151. <section>
  152. <label for="colorx">颜色:</label>
  153. <input type="color" list="colorsxx" id="colorx" />
  154. <datalist id="colorsxx">
  155. <option>#ff0000</option>
  156. <option>#ee0000</option>
  157. <option>#dd0000</option>
  158. <option>#cc0000</option>
  159. <option>#bb0000</option>
  160. </datalist>
  161. </section>
  162. <section>
  163. <label for="numberx">数值:</label>
  164. <input
  165. type="number"
  166. min="0"
  167. max="64"
  168. step="2"
  169. list="numbersxx"
  170. id="numberx"
  171. />
  172. <datalist id="numbersxx">
  173. <option>0</option>
  174. <option>2</option>
  175. <option>4</option>
  176. <option>8</option>
  177. <option>16</option>
  178. <option>32</option>
  179. <option>64</option>
  180. </datalist>
  181. </section>
  182. <section>
  183. <label for="urlx">URL:</label>
  184. <input type="url" list="urlsxx" id="urlx" />
  185. <datalist id="urlsxx">
  186. <option>https://developer.mozilla.org</option>
  187. <option>https://caniuse.com/</option>
  188. <option>https://mozilla.com</option>
  189. <option>https://mdn.github.io</option>
  190. <option>https://www.youtube.com/user/firefoxchannel</option>
  191. </datalist>
  192. </section>
  193. <section>
  194. <label for="rangex">范围:</label>
  195. <input type="range" min="0" max="64" list="numbersxx" id="rangex" />
  196. </section>
  197. </form>
  198. </body>
  199. </html>

按钮示例

代码如下

  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. section:last-child {
  32. margin: 0 auto;
  33. }
  34. button {
  35. height: 30px;
  36. width: 50px;
  37. border: none;
  38. outline: none;
  39. }
  40. button:hover {
  41. background-color: lightseagreen;
  42. color: white;
  43. cursor: pointer;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <h3>登录/注册</h3>
  49. <form>
  50. <section>
  51. <label for="email">邮箱:</label>
  52. <input type="email" name="email" id="email" required autofocus />
  53. </section>
  54. <section>
  55. <label for="password">密码:</label>
  56. <input type="password" name="password" id="password" required />
  57. </section>
  58. <section>
  59. <!-- 注册与登录,使用不同的脚本进行处理,并动态设置提交类型,打开方式 -->
  60. <button
  61. type="submit"
  62. formaction="login.php"
  63. formmethod="POST"
  64. formtarget="_blank"
  65. >
  66. 登录
  67. </button>
  68. <button
  69. type="submit"
  70. formaction="register.php"
  71. formmethod="GET"
  72. formtarget="_blank"
  73. >
  74. 注册
  75. </button>
  76. </section>
  77. </form>
  78. </body>
  79. </html>

文本域示例

代码如下

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