HTML5中form表单标签用法详解

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

语法:

结束,表单都必须放在其之间。

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

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

form    method=”post”   action=”save.php”>  

        label for=”username”>用户名:label>  

        input type=”text” name=”username” />  

        label for=”pass”>密码:label>  

立即学习“前端免费学习笔记(深入)”;

        input type=”password” name=”pass” />  

form>  

文本输入框,密码输入框

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

语法:

form>  

    input type = “text/password” name = “名称” value = “文本” />  

form>  

讲解:1.type :

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

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

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

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

nbsp;HTML>  

html>  

head>  

    meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>  

    title>文本输入框、密码输入框title>  

head>  

body>  

form  method=”post” action=”save.php”>  

    账户:  

    input type = “text” name = “myName” />  

    br />  

    密码:  

    input type = “password ”  name = “pass”/>  

form>  

body>  

html>  

结果:

账户:  
密码:  

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

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

语法:

textarea rows = “行数” cols = “列数” > 文本  textarea>  

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

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

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

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

nbsp;HTML>  

html>  

head>  

    meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>  

    title>文本域title>  

head>  

body>  

form method = “post” action = “save.php”>  

    label>个人简介label>  

    textarea rows = “5” cols = “10”>在这里输入内容…textarea>span>  

    input type = “submit” value = “确定” name = “submit” />  

    input type = “reset” value = “重置” name = “reset” />  

form>  

body>  

html>  

结果:

个人简介   

在后面会有详解。

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

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

input type = “radio/checkbox” value = “值” name = “名称” checked = “checked” />  

讲解:

1. type : radio :控件单选框

    checkbox : 控件复选框

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

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

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

nbsp;HTML>  

html>  

head>  

    meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>  

    title>单选框、复选框title>  

head>  

body>  

form name = “iForm”  method = “post” action = “save.php”>  

    你是否喜欢旅游?br />  

    input type = “radio” name = “radioLove” value = “喜欢” checked = “checked”/>span>  

    input type = “radio” name = “radioLove” value = “不喜欢”/>  

    input type = “radio” name = “radioLove” value = “无所谓”/>  

    br /> br />  

    你对那些运动感兴趣?br />  

    input type = “checkbox” name = “checkbox1” value = “跑步”/>  

    input type = “checkbox” name = “checkbox1” value = “打球” checked = “checked”/>  

    input type = “checkbox” name = “checkbox1” value = “登山” checked = “checked”/>  

    input type = “checkbox” name = “checkbox1” value = “健身” />  

  

form>  

body>  

html>  

结果:

你是否喜欢旅游?
   

你对那些运动感兴趣?
   

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

下拉列表框

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

单选:

nbsp;HTML>  

html>  

head>  

    meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>  

    title>下拉页表框title>  

head>  

body>  

form name = “iForm”  method = “post” action = “save.php”>  

    label>爱好:label>  

    select>  

        option value = “读书”>读书option>span>  

        option value = “运动”>运动option>  

        option value = “音乐”>音乐option>  

        option value = “旅游”>旅游option>  

        option value = “购物”>购物option>  

    span style=”color:#ff0000;”>select>span>  

  

form>  

body>  

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属性值一定要相同;

form>  

  label for=”male”>男label>  

  input type=”radio” name=”gender” span style=”color:#ff0000;”>id=”male”span> />  

  br />  

  label for=”female”>女label>  

  input type=”radio” name=”gender” span style=”color:#990000;”>id=”female”span> />  

  label for=”email”>输入你的邮箱地址label>  

  input type=”email” span style=”color:#ff6666;”>id=”email”span> placeholder=”Enter email”>  

form>  

结果:

男  
女  
输入你的邮箱地址 

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

nbsp;HTML>  

html>  

head>  

    meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>  

    title>form中的lable标签title>  

head>  

  

body>  

form>  

    你对什么运动感兴趣:br/>  

    label for = “sport1”>慢跑label>  

    input type = “checkbox” name = “sports” id = “sport1″/>  

    br />  

    label for = “sport2”>登山label>  

    input type = “checkbox” name = “sports” id = “sport2″/>  

    br />  

    label for = “sport3”>篮球label>  

    input type = “checkbox” name = “sports” id = “sport3″/>                     br />  

form>  

  

body>  

html>  

结果:

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

相关推荐:

动态生成form表单实现方法 

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

HTML Form表单元素的详解

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1545900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月21日 17:36:54
vue文件中HTML代码格式化方法
下一篇 2025年12月21日 17:37:04

相关推荐

发表回复

登录后才能评论
关注微信