Blogger Information
Blog 27
fans 0
comment 0
visits 18804
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
FORM表单
茂林
Original
769 people have browsed it

一. 表单概念

表单:是一个复合元素,用多个标签描述

二. 常用标签

1 <form></form>:表单控件容器

1.1 form.action:后端处理的脚本属性
1.2 form.method:表单数据提交方式,默认为 GET
(1)GET:数据以键值对方式,添加到 url 中,适合数量少且可公开的数据,如页码
(2)POST:数据在请求体中, url 不可见,适合大量或敏感的数据,如密码,文件上传
1.3 form.enctype:表单数据编码方案
(1)application/x-www-form-urlencoded: 默认对值对的编码方案
(2)multipart/form-data: 分块,原始数据,适用文件上传
1.4 form.target:_self(默认,在本页打开链接),_blank,和 a.target 相同,新页面打开链接
1.5 form.id:表单引用
1.6 form.onsubmit:表单提交事件,返回 false,可拦截提交请求

  1. <form
  2. action="login.php"
  3. method="post"
  4. enctype="application/x-www-form-urlencoded"
  5. target="_self"
  6. id="login"
  7. ></form>

2 <fieldset></fieldset>:表单分组容器

3 <legend></legend>:控件标签名称,字段集图例元素

4 <label></label>:表示用户界面中某个元素的说明

备注:label.for 属性 === input.id 属性 的值进行绑定(值相同),光标绑定。

  1. <!-- style="display: inline-grid; gap: 1em" fieldset容器的样式 -->
  2. <fieldset style="display: inline-grid; gap: 1em">
  3. <legend>用户登录</legend>
  4. <div class="username">
  5. <label for="username">账号:</label>
  6. <input type="text" name="u_name" id="username" />
  7. </div>
  8. <div class="psd">
  9. <label for="psd">密码:</label>
  10. <input type="password" name="psd" id="psd" />
  11. </div>
  12. <div class="button">
  13. <button>登录</button>
  14. </div>
  15. </fieldset>

以上合成的效果如下
以上合成的效果如下
login

<input >输入控件,类型由 type 属性决定

常用属性如下
5.1 input.type="text": 输入单行文本(默认)
5.2 input.type="email":邮箱控件
5.3 input.type="password":密码控件(密文)
5.4 input.type="number":数值控件 value 属性默认值,min 属性最小值,max 属性最大值 step 属性步长
5.5 input.type="date":日期控件 (除 date 值以外,还有 month,week,time,datetime-local)
5.6 input.type="color": 拾色器
5.7 input.type="url": URL 控件
5.8 input.type="search": 搜索框控件
5.9 input.type="hidden": 隐藏域控件
5.10 input.type="file": 文本域控件
5.11 input.type="radio": 单选按钮
5.12 input.type="checkbox": 复选框
5.13 select.name+option.value: 下拉列表框
5.14 input.list+datalist.id: 预定义列表框
5.15 textarea.cols/rows: 文本域(多行文本框)


5.16 input.name:表单的控件名称,作为键值对的一部分与表单一同提交
5.17 input.placeholder:当没有值设定时,出现在表单控件上的文字
5.18 input.required:布尔值,如果存在,一个值是必需的,或者必须勾选该值才能提交表格。
5.19 input.autofocus:自动焦点,文档或对话框中只有一个元素可以具有自动对焦属性。如果应用于多个元素,第一个将获得焦点

6 button 按钮,默认同步提交(type=”submit”)

6.1 button.type: 按钮(默认提交:type=”submit”)
6.2 button.disabled:布尔属性,存在就是 true,不写就是 false

  1. <form action="xxx.php" method="post">
  2. <fieldset style="display: inline-grid; gap: 1em">
  3. <legend>用户注册</legend>
  4. <!-- input.type = text 单行文本控件 -->
  5. <div class="username">
  6. <label for="uname">用户:</label>
  7. <input
  8. type="text"
  9. name="username"
  10. id="uname"
  11. placeholder="username"
  12. id="uname"
  13. autofocus
  14. required
  15. />
  16. </div>
  17. <div class="myemail">
  18. <label for="myemail">邮箱:</label>
  19. <!-- input.type = email 邮箱控件使用 -->
  20. <input
  21. type="email"
  22. name="email"
  23. id="myemail"
  24. placeholder="username@email.com"
  25. required
  26. />
  27. </div>
  28. <div class="psw">
  29. <label for="psw">密码:</label>
  30. <!-- input.type = password密码控件的使用 -->
  31. <!-- onkeydown="this.nextElementSibling.disabled=false"
  32. 密码显示与隐藏的脚本 -->
  33. <input
  34. type="password"
  35. name="password"
  36. id="psw"
  37. placeholder="******"
  38. required
  39. onkeydown="this.nextElementSibling.disabled=false"
  40. />
  41. <!-- disabled: 布尔属性, 存在就是true,不写就是false -->
  42. <!-- 事件属性: on+事件名称, 点击事件属性: onclick -->
  43. <button type="button" disabled onclick="showPsw(this,this.form)">
  44. 显示
  45. </button>
  46. </div>
  47. <!-- input.type = number数值类控件 -->
  48. <div class="age">
  49. <label for="age">年龄:</label>
  50. <input
  51. type="number"
  52. name="age"
  53. id="age"
  54. value="20"
  55. min="20"
  56. max="70"
  57. step="2"
  58. />
  59. </div>
  60. <!-- input.type = date日期控件 -->
  61. <div class="birthday">
  62. <label for="birthday">生日:</label>
  63. <input
  64. type="date"
  65. name="birthday"
  66. id="birthday"
  67. value="2000-01-01"
  68. min="1949-10-1"
  69. max="2001-0=12-31"
  70. />
  71. <input type="month" name="" id="" />
  72. <input type="week" />
  73. <input type="time" />
  74. <input type="datetime-local" />
  75. </div>
  76. <!-- input.type = color拾色器 -->
  77. <div class="color">
  78. <label for="color">背景:</label>
  79. <input
  80. type="color"
  81. name="bgcolor"
  82. id="color"
  83. value="#ffff00"
  84. onchange="setBgColor(this,this.form)"
  85. />
  86. </div>
  87. <div class="search">
  88. <label for="search">搜索:</label>
  89. <input
  90. type="search"
  91. name="keywords"
  92. id="search"
  93. placeholder="输入关键字"
  94. />
  95. </div>
  96. <!-- input.type = file 文件上传 控件 -->
  97. <div class="upload">
  98. <label for="upload">头像:</label>
  99. <input
  100. type="file"
  101. name="user_pic"
  102. id="upload"
  103. accept="image/jpeg,image/png"
  104. />
  105. <button type="button" onclick="fileUploads(this.form)">上传</button>
  106. </div>
  107. <!-- input.type = radio 单选按钮 控件 -->
  108. <div class="gender">
  109. <!-- 推荐与默认值的input.id绑定 ,如果操作中忘记默认值,单击性别即可还原默认值-->
  110. <label for="secret">性别:</label>
  111. <!-- name="male",将这个键值对,做为变量与值,提交到后端脚本中处理 -->
  112. <!-- name: 必须相同,以保住唯一性 -->
  113. <input type="radio" name="gender" value="male" id="male" /><label
  114. for="male"
  115. ></label
  116. >
  117. <input type="radio" name="gender" value="female" id="female" /><label
  118. for="female"
  119. ></label
  120. >
  121. <input type="radio" name="gender" value="secret" id="secret" /><label
  122. for="secret"
  123. >保密</label
  124. >
  125. </div>
  126. <!-- input.type = checkbox 复选框 控件 -->
  127. <div class="hibby">
  128. <label>爱好:</label>
  129. <!--
  130. * 1. name: hobby[], 数组的语法形式,用于保存一个或多个值,这样后端处理脚本就可以获取到多个值
  131. * 2. input.value <=> input.id <=> label.for,其实只需要input.id <==> label.for
  132. * 3. checked: 默认选项
  133. -->
  134. <input type="checkbox" name="hib[]" value="game" id="game" checked />
  135. <label for="game">游戏</label>
  136. <input type="checkbox" name="hib[]" value="read book" id="read book" />
  137. <label for="read book">阅读</label>
  138. <input type="checkbox" name="hib[]" value="football" id="football" />
  139. <label for="football">足球</label>
  140. <input type="checkbox" name="hib[]" value="trave" id="trave" />
  141. <label for="trave">旅游</label>
  142. </div>
  143. <!-- `select.name+option.value`: 下拉列表框 控件 -->
  144. <div class="edu">
  145. <label for="edu">学历:</label>
  146. <!--
  147. * 1. name与value: 名值,分布在二个标签中,select.name , option.value
  148. * 2. selected: 默认选择
  149. * 3. 选择过多,且有规律,建议分组展示: optgroup
  150. * 4. 为select加提示: selected + disabled, 加到第一项之前
  151. * 5. multiple: 支持多选
  152. -->
  153. <!-- 下拉列表框, 将键与值所在的标签进行分离 -->
  154. <!-- select.name = select>option.value -->
  155. <!-- select.form="指向当前表单元素" -->
  156. <select name="edu" id="edu" form="">
  157. <!-- <select name="edu" id="edu" multiple> -->
  158. <!-- select 标签不能加提示, 想加怎么办 -->
  159. <!-- 加提示 -->
  160. <option value="" selected disabled>--请选择--</option>
  161. <option value="0">文盲</option>
  162. <!-- 下拉选项分组: 同类组合在一起 -->
  163. <optgroup label="义务教育">
  164. <option value="1">小学</option>
  165. <option value="2">中学</option>
  166. <option value="3">高中</option>
  167. </optgroup>
  168. <optgroup label="高等教育">
  169. <option value="4">专科</option>
  170. <option value="5">本科</option>
  171. <option value="6">硕士</option>
  172. <option value="7">博士</option>
  173. </optgroup>
  174. </select>
  175. </div>
  176. <!-- 预定义列表框 -->
  177. <div class="like">
  178. <label for="keyword">语言: </label>
  179. <input type="search" name="language" list="details" id="keyword" />
  180. <!-- 预置值列表 -->
  181. <!-- ? input.list <==> datalist.id 进行绑定 -->
  182. <datalist id="details">
  183. <option value="html">html</option>
  184. <option value="css">css</option>
  185. <option value="js">js</option>
  186. <option value="php">php</option>
  187. <option value="vue">vue</option>
  188. <option value="node">node</option>
  189. </datalist>
  190. </div>
  191. <!-- 文本框: 多行文本框 -->
  192. <div>
  193. <label for="comment">备注:</label>
  194. <!-- ? textarea: 没有 value 属性,它的值位于textarea标签之间 -->
  195. <!-- ? maxlength: 最大长度 -->
  196. <textarea
  197. name="comment"
  198. id="comment"
  199. cols="30"
  200. rows="5"
  201. maxlength="200"
  202. style="resize: none"
  203. >
  204. Hello world
  205. </textarea
  206. >
  207. </div>
  208. <button>提交注册</button>
  209. </fieldset>
  210. </form>
  211. <!-- 密码显示与隐藏脚本 -->
  212. <script>
  213. function showPsw(ele, form) {
  214. const psw = form.password;
  215. if (psw.type === "password") {
  216. psw.type = "text";
  217. ele.textContent = "隐藏";
  218. } else if (psw.type === "text") {
  219. psw.type = "password";
  220. ele.textContent = "显示";
  221. } else {
  222. return false;
  223. }
  224. }
  225. // 设置表单背景
  226. function setBgColor(ele, form) {
  227. form.firstElementChild.style.backgroundColor = ele.value;
  228. }
  229. // 文件上传
  230. function fileUploads(form) {
  231. let tips = "";
  232. if (form.user_pic.value.length === 0) {
  233. tips += "文件不能为空";
  234. } else {
  235. tips += "上传成功";
  236. }
  237. alert(tips);
  238. }
  239. </script>

最后的效果如图
显示的结果
结果
2023-1-16

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:下次作业记得在提交的时候,在状态一栏选择作业
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!