HTML5中form表单标签用法详解

小云云
发布: 2018-03-05 14:50:03
原创
2749人浏览过

本文主要和大家分享HTML5中form表单标签用法详解,会以代码实例来和大家分享form的用法,希望能帮助到大家。

语法:

讲解:1.

是成对出现的, 以
开始, 以
结束,表单都必须放在其之间。

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

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

  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>  

文本输入框,密码输入框

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

语法:

  1. form>  

  2.     input type = "text/password" name = "名称" value = "文本" />  

  3. form>  


讲解:1.type :

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

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

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

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

  1. nbsp;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>  

结果:

账户:  
密码:  

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

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

语法:

textarea rows = "行数" cols = "列数" > 文本  textarea>  


讲解:1.文本输入域以 结束

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

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

4.在标签之间输入默认值

  1. nbsp;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>  

结果:

个人简介   

在后面会有详解。


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

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

  1. input type = "radio/checkbox" value = "值" name = "名称" checked = "checked" />  

讲解:


1. type : radio :控件单选框

    checkbox : 控件复选框

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

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

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

  1. nbsp;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的取值一定要一致,这样同一组的单选按钮才可以起到单选的作用。



下拉列表框

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

单选:

  1. nbsp;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>  

结果:爱好:  读书运动音乐 旅游购物  (可以下拉)


提交值:是向服务器提交的值

选项:是显示的值

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

多选:

就将上面的就行,然后在widows下ctrl ,同时单击,在Mac 下 Command + 单击 

使用提交按钮,提交数据

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

语法:

讲解:

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

2.value: 按钮上显示的字

重置 

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

讲解:

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

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

label标签

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

语法:

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

  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>  


结果:

  
  
输入你的邮箱地址 

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

  1. nbsp;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>  

结果:

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

相关推荐:

动态生成form表单实现方法 

JavaScript实现动态添加Form表单元素的方法示例

HTML Form表单元素的详解

以上就是HTML5中form表单标签用法详解的详细内容,更多请关注php中文网其它相关文章!

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号