HTML5 form表单标签

语法: <form method = "传送方式" action = "服务器文件">

讲解:1.<form>是成对出现的, 以<form> 开始, 以 </form>结束,表单都必须放在其之间。

  2.method 传送方式,  get/post  是后端程序员考虑的问题

  3.action  浏览者输入的数据被传送到的地方,比如一个php页面, (save.php)  

[html]  view plain  copy
  1. <form    method="post"   action="save.php">  
  2.         <label for="username">用户名:</label>  
  3.         <input type="text" name="username" />  
  4.         <label for="pass">密码:</label>  
  5.         <input type="password" name="pass" />  
  6. </form>  


文本输入框,密码输入框

当用户需要在表单中键入字母,数据等,就要用到文本输入框,文本输入框也可以转化为密码输入框

语法:

[html]  view plain  copy
  1. <form>  
  2.     <input type = "text/password" name = "名称" value = "文本" />  
  3. </form>  

讲解:1.type :

扫描二维码关注公众号,回复: 3506228 查看本文章

当 type 为 text时,为文本输入框

当 type 为 password 时, 为密码输入框

2.name :为文本框命名,以备后台asp php使用

3.value :为文本输入框设置默认值(一般起提示作用)

[html]  view plain  copy
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.     <title>文本输入框、密码输入框</title>  
  6. </head>  
  7. <body>  
  8. <form  method="post" action="save.php">  
  9.     账户:  
  10.     <input type = "text" name = "myName" />  
  11.     <br />  
  12.     密码:  
  13.     <input type = "password "  name = "pass"/>  
  14. </form>  
  15. </body>  
  16. </html>  
结果:

账户:  
密码:  

文本域:支持多行文本输入

当用户需要在表单中输入大段文字时,就要用到文本输入域

语法:

[html]  view plain  copy
  1. <textarea rows = "行数" cols = "列数" > 文本  </textarea>  

讲解:1.文本输入域以 <textarea>开始 ,以 </textarea>结束

2.rows: 输入文本输入域的行数

3.cols : 输入文本输入域的列数

4.在<textarea></textarea>标签之间输入默认值

[html]  view plain  copy
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.     <title>文本域</title>  
  6. </head>  
  7. <body>  
  8. <form method = "post" action = "save.php">  
  9.     <label>个人简介</label>  
  10.     <textarea rows = "5" cols = "10">在这里输入内容...</textarea></span>  
  11.     <input type = "submit" value = "确定" name = "submit" />  
  12.     <input type = "reset" value = "重置" name = "reset" />  
  13. </form>  
  14. </body>  
  15. </html>  

结果:

个人简介   

<lable>在后面会有详解。


使用单选框,复选框让用户选择

在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好办法,在HTML中,有单选框和复选框,两者的主要区别是 单选框中用户的选项只能选择一项,而复选框中用户可以任意选择多项,甚至全选。

[html]  view plain  copy
  1. <input type = "radio/checkbox" value = "值" name = "名称" checked = "checked" />  
讲解:

1. type : radio :控件单选框

    checkbox : 控件复选框

2. value: 提供数据到服务器的值

3. name:为控件命名,以备后台程序ASP,PHP使用

4.checked: 当设置 checked = “checked”时,该选项被默认选中。

 

[html]  view plain  copy
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.     <title>单选框、复选框</title>  
  6. </head>  
  7. <body>  
  8. <form name = "iForm"  method = "post" action = "save.php">  
  9.     你是否喜欢旅游?<br />  
  10.     <input type = "radio" name = "radioLove" value = "喜欢" checked = "checked"/></span>  
  11.     <input type = "radio" name = "radioLove" value = "不喜欢"/>  
  12.     <input type = "radio" name = "radioLove" value = "无所谓"/>  
  13.     <br /> <br />  
  14.     你对那些运动感兴趣?<br />  
  15.     <input type = "checkbox" name = "checkbox1" value = "跑步"/>  
  16.     <input type = "checkbox" name = "checkbox1" value = "打球" checked = "checked"/>  
  17.     <input type = "checkbox" name = "checkbox1" value = "登山" checked = "checked"/>  
  18.     <input type = "checkbox" name = "checkbox1" value = "健身" />  
  19.   
  20. </form>  
  21. </body>  
  22. </html>  

结果:

你是否喜欢旅游?
   

你对那些运动感兴趣?
   


注意:同一组的单选按钮,name的取值一定要一致,这样同一组的单选按钮才可以起到单选的作用。



下拉列表框

使用下拉列表框,节省空间,既可以单选,又可以多选。

单选:


[html]  view plain  copy
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.     <title>下拉页表框</title>  
  6. </head>  
  7. <body>  
  8. <form name = "iForm"  method = "post" action = "save.php">  
  9.     <label>爱好:</label>  
  10.     <select>  
  11.         <option value = "读书">读书</option></span>  
  12.         <option value = "运动">运动</option>  
  13.         <option value = "音乐">音乐</option>  
  14.         <option value = "旅游">旅游</option>  
  15.         <option value = "购物">购物</option>  
  16.     <span style="color:#ff0000;"></select></span>  
  17.   
  18. </form>  
  19. </body>  
  20. </html>  
结果:爱好:   读书运动音乐 旅游购物   (可以下拉)

<option value =  "提交值" > 选项</option>
提交值:是向服务器提交的值

选项:是显示的值

设置 selected = "selected" 则该选项默认被选中。


多选:

就将上面的<select> 改为 <select multiple = "multiple" >就行,然后在widows下ctrl ,同时单击,在Mac 下 Command + 单击 



使用提交按钮,提交数据

在表单中有两种按钮可以使用,提交按钮和重置,当用户需要提交信息到服务器时,需要用到提交按钮。

语法:

<input type = "submit" value = "提交">

讲解:

1.只有当type = "sumit"时,按钮才有提交的作用

2.value: 按钮上显示的字


重置 

 当用户需要重置表单信息到初始状态时,可以使用重置按钮,只要把type 改为 reset 就行。

<input type = "reset" value = "重置">

讲解:

1.同理提交按钮,只有当type = "reset"时, 按钮才有重置的作用

2.value : 按钮上显示的文字


label标签

label标签不会向用户呈现任何特殊的效果,它的作用是为鼠标用户改进了可用性,如果你在label标签内点击文本,就会触发此控件,也就是说,当用户单击选中该label标签时,浏览器会自动将焦点转到和 标签相关的表单控件上(就自动选中和该label标签相关联的表单控件上);

语法:

<label  for = "控件id 的名称" >

注意:标签中for 属性的值应该与相关控件的id属性值一定要相同;

[html]  view plain  copy
  1. <form>  
  2.   <label for="male"></label>  
  3.   <input type="radio" name="gender" <span style="color:#ff0000;">id="male"</span> />  
  4.   <br />  
  5.   <label for="female"></label>  
  6.   <input type="radio" name="gender" <span style="color:#990000;">id="female"</span> />  
  7.   <label for="email">输入你的邮箱地址</label>  
  8.   <input type="email" <span style="color:#ff6666;">id="email"</span> placeholder="Enter email">  
  9. </form>  

结果:

  
  
输入你的邮箱地址 

以下是自己仿写的,可复选的:

[html]  view plain  copy
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5.     <title>form中的lable标签</title>  
  6. </head>  
  7.   
  8. <body>  
  9. <form>  
  10.     你对什么运动感兴趣:<br/>  
  11.     <label for = "sport1">慢跑</label>  
  12.     <input type = "checkbox" name = "sports" id = "sport1"/>  
  13.     <br />  
  14.     <label for = "sport2">登山</label>  
  15.     <input type = "checkbox" name = "sports" id = "sport2"/>  
  16.     <br />  
  17.     <label for = "sport3">篮球</label>  
  18.     <input type = "checkbox" name = "sports" id = "sport3"/>                     <br />  
  19. </form>  
  20.   
  21. </body>  
  22. </html>  
结果:

你对什么运动感兴趣:
慢跑  
登山  
篮球  

猜你喜欢

转载自blog.csdn.net/qq_25205059/article/details/79434211