参考:javascript
目录:php
HTML 表单用于搜集不一样类型的用户输入。css
HTML 表单用于收集用户输入。html
<form> 元素定义 HTML 表单。html5
<form> 标签用于为用户输入建立 HTML 表单。java
表单可以包含 input 元素,好比文本字段、复选框、单选框、提交按钮等等。jquery
表单还能够包含 menus、textarea、fieldset、legend 和 label 元素(<label>元素通常与<input type="radio">和<input type="checkbox">结合使用较多)。web
<label>元素举例:正则表达式
<label>标签是<input>的描述,它自己不会有特殊效果,但它和其它<input>标签使用能够提高用户的使用体验,用户不用非得点 击到按钮,而是点击文字便可选中,如“记住密码”,<label>和<input>进行了关联,可是提交的依然是value的值,<label>中的内容不会被提交(经过<label>标签的for属性指向<input>标签的id来进行关联)。浏览器
1 <html> 2 <body> 3 <p>请点击文本标记之一,就能够触发相关控件:</p> 4 5 <form> 6 <label for="male">Male</label> 7 <input type="radio" name="sex" id="male" /> 8 <br /> 9 <label for="female">Female</label> 10 <input type="radio" name="sex" id="female" /> 11 </form> 12 </body> 13 </html>
<legend>元素举例(一般和<fieldset>元素结合使用):
1 <form> 2 <fieldset> 3 <legend>Student Information</legend> 4 Name: <input type="text" name="Name"> 5 <br /> 6 Sex: 7 <label for="boy1">boy</label> 8 <input type="radio" name="Sex" id="boy1"> 9 <label for="girl1">girl</label> 10 <input type="radio" name="Sex" id="girl1"> 11 </fieldset> 12 </form>
输出结果:
表单用于向服务器传输数据。
<form>表单元素属性:
属性 | 值 | 描述 |
---|---|---|
accept | MIME_type | HTML 5 中不支持。 |
accept-charset | charset_list | 规定服务器可处理的表单数据字符集。 |
action | URL | 规定当提交表单时向何处发送表单数据。 |
autocomplete⭐ |
|
规定是否启用表单的自动完成功能。 |
enctype | 见说明 | 规定在发送表单数据以前如何对其进行编码(如X进制)。 enctype 属性可能的值:
|
(见本文第2节说明) |
|
规定用于发送 form-data 的 HTTP 方法。 |
name | form_name | 规定表单的名称。 |
novalidate⭐ | novalidate | 若是使用该属性,则提交表单时不进行验证。 |
target |
|
规定在何处打开 acti |
HTML <form> 元素,已设置全部可能的属性,实例:
1 <form action="action_page.php" method="GET" target="_blank" accept-charset="UTF-8" 2 ectype="application/x-www-form-urlencoded" autocomplete="off" novalidate> 3 <!--form elements--> 4 </form>
输出结果:略。
(1)属性包括
value 属性:value 属性规定输入字段的初始值:
1 <form action=""> 2 First name:<br> 3 <input type="text" name="firstname" value="John"> 4 <br> 5 Last name:<br> 6 <input type="text" name="lastname"> 7 </form>
readonly 属性:readonly 属性规定输入字段为只读(不能修改),readonly 属性不须要值。它等同于 readonly="readonly"。:
1 <form action=""> 2 First name:<br> 3 <input type="text" name="firstname" value="John" readonly> 4 <br> 5 Last name:<br> 6 <input type="text" name="lastname"> 7 </form>
输出结果:
First name:
Last name:
disabled 属性:disabled 属性规定输入字段是禁用的。被禁用的元素是不可用和不可点击的。被禁用的元素不会被提交。disabled 属性不须要值。它等同于 disabled="disabled"。
1 <form action=""> 2 First name:<br> 3 <input type="text" name="firstname" value ="John" disabled> 4 <br> 5 Last name:<br> 6 <input type="text" name="lastname"> 7 </form>
输出结果:
First name:
Last name:
size 属性:size 属性规定输入字段的尺寸(以字符计):
1 <form action=""> 2 First name:<br> 3 <input type="text" name="firstname" value ="John" size="40"> 4 <br> 5 Last name:<br> 6 <input type="text" name="lastname"> 7 </form>
输出结果:
First name:
Last name:
maxlength 属性:maxlength 属性规定输入字段容许的最大长度。如设置 maxlength 属性,则输入控件不会接受超过所容许数的字符。该属性不会提供任何反馈。若是须要提醒用户,则必须编写 JavaScript 代码。
注释:输入限制并不是万无一失。JavaScript 提供了不少方法来增长非法输入。如需安全地限制输入,则接受者(服务器)必须同时对限制进行检查。
1 <form action=""> 2 First name:<br> 3 <input type="text" name="firstname" maxlength="10"> 4 <br> 5 Last name:<br> 6 <input type="text" name="lastname"> 7 </form>
输出结果:
First name:
Last name:
image属性(这种方式使用的很少,image提交可能会出现提交两次的问题):
1 <form action="/example/html5/demo_form.asp" method="get"> 2 First name: <input type="text" name="fname" /><br /> 3 Last name: <input type="text" name="lname" /><br /> 4 <input type="image" src="/i/eg_submit.jpg" alt="Submit" width="128" height="128"/> 5 </form> 6 7 <p><b>注释:</b>默认地,image 输入类型会发生点击图像按钮时的 X 和 Y 坐标。</p>
输出结果:
注释:默认地,image 输入类型会发生点击图像按钮时的 X 和 Y 坐标。
(2)HTML5 <input>元素属性
属性 | 值 | 描述 |
---|---|---|
accept | mime_type | 规定经过文件上传来提交的文件的类型。 |
align |
|
不同意使用。规定图像输入的对齐方式。 |
alt | text | 定义图像输入的替代文本。 |
autocomplete⭐ |
|
规定是否使用输入字段的自动完成功能。 |
autofocus⭐ | autofocus | 规定输入字段在页面加载时是否得到焦点。 (不适用于 type="hidden") |
checked | checked | 规定此 input 元素首次加载时应当被选中。 |
disabled | disabled | 当 input 元素加载时禁用此元素。 |
form⭐ | formname | 规定输入字段所属的一个或多个表单。 |
formaction⭐ | URL | 覆盖表单的 action 属性。 (适用于 type="submit" 和 type="image") |
formenctype⭐ | 见注释 | 覆盖表单的 enctype 属性。 (适用于 type="submit" 和 type="image") |
formmethod⭐ |
|
覆盖表单的 method 属性。 (适用于 type="submit" 和 type="image") |
formnovalidate⭐ | formnovalidate | 覆盖表单的 novalidate 属性。 若是使用该属性,则提交表单时不进行验证。 |
formtarget⭐ |
|
覆盖表单的 target 属性。 (适用于 type="submit" 和 type="image") |
height⭐ |
|
定义 input 字段的高度。(适用于 type="image") |
list⭐ | datalist-id | 引用包含输入字段的预约义选项的 datalist 。 |
max⭐ |
|
规定输入字段的最大值。 请与 "min" 属性配合使用,来建立合法值的范围。 |
maxlength⭐ | number | 规定输入字段中的字符的最大长度。 |
min⭐ |
|
规定输入字段的最小值。 请与 "max" 属性配合使用,来建立合法值的范围。 |
multiple⭐ | multiple | 若是使用该属性,则容许一个以上的值。 |
name⭐ | field_name | 定义 input 元素的名称。 |
pattern⭐ | regexp_pattern | 规定输入字段的值的模式或格式。 例如 pattern="[0-9]" 表示输入值必须是 0 与 9 之间的数字。 |
placeholder⭐ | text | 规定帮助用户填写输入字段的提示。 |
readonly⭐ | readonly | 规定输入字段为只读。 |
required⭐ | required | 指示输入字段的值是必需的。 |
size⭐ | number_of_char | 定义输入字段的宽度。 |
src⭐ | URL | 定义以提交按钮形式显示的图像的 URL。 |
step⭐ | number | 规定输入字的的合法数字间隔。 |
type⭐ |
|
规定 input 元素的类型。 注意file类型使用(一、<form>表单的method属性(见本文第2节说明)值要是post,不受大小限制;二、要加enctype="multipart/form-data"属性,即说明文件以二进制方式显示、传输):
1 <form method="post" enctype="multipart/form-data"> 2 <input type="file" name="fileField" /> 3 <input type="submit" value="上传" /> 4 </form>
|
value | value | 规定 input 元素的值。 |
width⭐ |
|
定义 input 字段的宽度。(适用于 type="image") |
增长:<input type="search"> 用于搜索字段(搜索字段的表现相似常规文本字段)
<input> 元素是最重要的表单元素。
<input> 元素有不少形态,根据不一样的 type 属性。
这是本章中使用的类型:
类型 | 描述 |
---|---|
text(默认) | 定义常规文本输入。在<form>表单元素中,<input type="text">和<input>是相同的效果 |
radio | 定义单选按钮输入(选择多个选择之一) |
submit | 定义提交按钮(提交表单) |
增长类型:password:password 字段中的字符会被作掩码处理(显示为星号或实心圆)。
增长类型:checkbox:定义复选框。复选框容许用户在有限数量的选项中选择零个或多个选项。
增长类型:button:定义按钮。
增长类型:image
HTML5 输入类型
HTML5 增长了多个新的输入类型type属性:
注释:老式 web 浏览器不支持的输入类型,会被视为输入类型 text。
HTML5 为 <input> 增长了以下属性:
并为 <form> 增长如需属性:
<input type="text"> 定义用于文本输入的单行输入字段:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 13 <form> 14 Text input 1:<br /> 15 <input type="text" name="textinput1"></input> 16 <br /> 17 Text input 2:<br /> 18 <input type="text" name="textinput2"></input> 19 </form> 20 21 <br /> 22 <form> 23 Text input 3: 24 <input type="text" name="textinput3"></input> 25 <br /> 26 Text input 4: 27 <input type="text" name="textinput4"></input> 28 </form> 29 30 </body> 31 </html>
输出结果:
注释:表单自己并不可见。还要注意文本字段的默认宽度是 20 个字符。
<input type="password">,password 字段中的字符会被作掩码处理(显示为星号或实心圆)。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 13 <form> 14 Text input 1:<br /> 15 <input type="text" name="textinput1"></input> 16 <br /> 17 Password input 2:<br /> 18 <input type="password" name="psd"></input> 19 </form> 20 21 </body> 22 </html>
输出结果:
Text input 1:
Password input 2:
<input type="radio"> 定义单选按钮。
单选按钮容许用户在有限数量的选项中选择其中之一(<input type="radio">增长name属性,name相同的radio即为一组,增长checked属性为默认选中的意思):
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 13 <form> 14 <input type="radio" name="sex" value="male" checked>Male</input> 15 <br /> 16 <input type="radio" name="sex" value="female">Female</input> 17 </form> 18 19 </body> 20 </html>
checked属性能够写成checked或者checked="checked"。
输出结果:
Male
Female
<input type="checkbox"> 定义复选框。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 13 <form action="/demo/demo_form.asp"> 14 <input type="checkbox" name="vehicle" value="Bike">I have a bike 15 <br> 16 <input type="checkbox" name="vehicle" value="Car">I have a car 17 <br><br> 18 <input type="submit"> 19 </form> 20 21 </body> 22 </html>
输出结果:
I have a bike
I have a car
<input type="submit"> 定义用于向表单处理程序(form-handler)提交表单的按钮。
表单处理程序一般是包含用来处理输入数据的脚本的服务器页面。
表单处理程序在表单的 action 属性中指定:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 13 <form action="action_page.php"> 14 Text input 1 15 <input type="text" name="textinput1" value="Mickey"></input> <!--value为显示默认值--> 16 <br /> 17 Text input 2 18 <input type="text" name="textinput2" value="Mouse"></input> <!--value为显示默认值--> 19 <br /><br /> 20 <input type="submit" value="Submit"></input> 21 </form> 22 23 </body> 24 </html>
输出结果:
Text input 1
Text input 2
<button> 元素定义可点击的按钮:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <form> 13 <button type="button" onclick='alert("Hello, world!")'>Click Me!</button> 14 </form> 15 16 </body> 17 </html>
输出结果(点击“Click Me!”按钮后,会出现网页中的alert提示):
<input type="number"> 用于应该包含数字值的输入字段。
您可以对数字作出限制。
根据浏览器支持,限制可应用到输入字段。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 数值约束会应用到输入字段中。 15 </p> 16 17 <form action="/demo/demo_form.asp"> 18 数量(1 到 5 之间): 19 <input type="number" name="quantity" min="1" max="5"> 20 <br /> 21 Quantity: 22 <input type="number" name="points" min="0" max="100" step="10" value="30"> 23 <br /> 24 <input type="submit"> 25 </form> 26 27 <p><b>注释:</b>IE9 及早期版本不支持 type="number"。</p> 28 </body> 29 </html>
输出结果:
根据浏览器支持:
数值约束会应用到输入字段中。
注释:IE9 及早期版本不支持 type="number"。
<input type="date"> 用于应该包含日期的输入字段。
根据浏览器支持,日期选择器会出现输入字段中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 当您填写输入字段时会弹出日期选择器。 15 </p> 16 17 <form action="/demo/demo_form.asp"> 18 生日: 19 <input type="date" name="bday"> 20 <br /> 21 Enter a date before 1980-01-01: 22 <input type="date" name="bday" max="1979-12-31"><br /> 23 Enter a date after 2000-01-01: 24 <input type="date" name="bday" min="2000-01-02"><br /> 25 <input type="submit"> 26 </form> 27 28 <p><b>注释:</b>Firefox 或者 29 Internet Explorer 11 以及更早版本不支持 type="date"。</p> 30 </body> 31 </html>
输出结果:
根据浏览器支持:
当您填写输入字段时会弹出日期选择器。
注释:Firefox 或者 Internet Explorer 11 以及更早版本不支持 type="date"。
<input type="color"> 用于应该包含颜色的输入字段。
根据浏览器支持,颜色选择器会出现输入字段中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 Depending on browser support:<br> 14 A color picker can pop-up when you enter the input field. 15 </p> 16 17 <form action="action_page.php"> 18 Select your favorite color: 19 <input type="color" name="favcolor" value="#ff0000"> 20 <input type="submit"> 21 </form> 22 23 <p><b>Note:</b> type="color" is not supported in Internet Explorer.</p> 24 </body> 25 </html>
输出结果:
Depending on browser support:
A color picker can pop-up when you enter the input field.
注释:type="color" is not supported in Internet Explorer.
<input type="range"> 用于应该包含必定范围内的值的输入字段。
根据浏览器支持,输入字段可以显示为滑块控件。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 输入类型 "range" 可显示为滑动控件。 15 </p> 16 17 <form action="/demo/demo_form.asp" method="get"> 18 Points: 19 <input type="range" name="points" min="0" max="10"> 20 <input type="submit"> 21 </form> 22 23 <p><b>注释:</b>IE9 及早期版本不支持 type="range"。</p> 24 </body> 25 </html>
输出结果:
根据浏览器支持:
输入类型 "range" 可显示为滑动控件。
注释:IE9 及早期版本不支持 type="range"。
<input type="month"> 容许用户选择月份和年份。
根据浏览器支持,日期选择器会出现输入字段中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 当您填写输入字段时会弹出日期选择器。 15 </p> 16 17 <form action="/demo/demo_form.asp"> 18 生日(月和年): 19 <input type="month" name="bdaymonth"> 20 <input type="submit"> 21 </form> 22 23 <p><b>注释:</b>Firefox 或者 24 Internet Explorer 11 以及更早版本不支持 type="month"。</p> 25 </body> 26 </html>
输出结果:
根据浏览器支持:
当您填写输入字段时会弹出日期选择器。
注释:Firefox 或者 Internet Explorer 11 以及更早版本不支持 type="month"。
<input type="week"> 容许用户选择周和年。
根据浏览器支持,日期选择器会出现输入字段中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 当您填写输入字段时会弹出日期选择器。 15 </p> 16 17 <form action="action_page.php"> 18 Select a week: 19 <input type="week" name="year_week"> 20 <input type="submit"> 21 </form> 22 23 <p><b>注释:</b> 24 Internet Explorer 不支持 type="week"。</p> 25 </body> 26 </html>
输出结果:
根据浏览器支持:
当您填写输入字段时会弹出日期选择器。
注释: Internet Explorer 不支持 type="week"。
<input type="time"> 容许用户选择时间(无时区)。
根据浏览器支持,时间选择器会出现输入字段中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 当您填写输入字段时会弹出日期选择器。 15 </p> 16 17 <form action="/demo/demo_form.asp"> 18 请选取一个时间: 19 <input type="time" name="usr_time"> 20 <input type="submit"> 21 </form> 22 23 <p><b>注释:</b>Firefox 或者 24 Internet Explorer 11 以及更早版本不支持 type="time"。</p> 25 </body> 26 </html>
输出结果:
根据浏览器支持:
当您填写输入字段时会弹出日期选择器。
注释:Firefox 或者 Internet Explorer 11 以及更早版本不支持 type="time"。
<input type="datetime"> 容许用户选择日期和时间(有时区)。
根据浏览器支持,日期选择器会出现输入字段中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 当您填写输入字段时会弹出日期选择器。 15 </p> 16 17 <form action="action_page.php"> 18 生日(日期和时间): 19 <input type="datetime" name="bdaytime"> 20 <input type="submit"> 21 </form> 22 23 <p><b>注释:</b>Chrome、Firefox 或 Internet Explorer 不支持 type="datetime"。</p> 24 </body> 25 </html>
输出结果:
根据浏览器支持:
当您填写输入字段时会弹出日期选择器。
注释:Chrome、Firefox 或 Internet Explorer 不支持 type="datetime"。
<input type="datetime-local"> 容许用户选择日期和时间(无时区)。
根据浏览器支持,日期选择器会出现输入字段中。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <p> 13 根据浏览器支持:<br> 14 当您填写输入字段时会弹出日期选择器。 15 </p> 16 17 <form action="/demo/demo_form.asp"> 18 生日(日期和时间): 19 <input type="datetime-local" name="bdaytime"> 20 <input type="submit" value="Send"> 21 </form> 22 23 <p><b>注释:</b>Firefox 或者 24 Internet Explorer 不支持 type="datetime-local"。</p> 25 </body> 26 </html>
输出结果:
根据浏览器支持:
当您填写输入字段时会弹出日期选择器。
注释:Chrome、Firefox 或 Internet Explorer 不支持 type="datetime"。
<input type="email"> 用于应该包含电子邮件地址的输入字段。
根据浏览器支持,可以在被提交时自动对电子邮件地址进行验证。
某些智能手机会识别 email 类型,并在键盘增长 ".com" 以匹配电子邮件输入。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <form action="/demo/demo_form.asp"> 13 E-mail: 14 <input type="email" name="email"> 15 <input type="submit"> 16 </form> 17 18 <p> 19 <b>注释:</b>IE9 及更早版本不支持 type="email"。</p> 20 </body> 21 </html>
输出结果:
注释:IE9 及更早版本不支持 type="email"。
<input type="search"> 用于搜索字段(搜索字段的表现相似常规文本字段)。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <form action="/demo/demo_form.asp"> 13 搜索谷歌: 14 <input type="search" name="googlesearch"> 15 <input type="submit"> 16 </form> 17 </body> 18 </html>
输出结果:
搜索谷歌:
<input type="tel"> 用于应该包含电话号码的输入字段。
目前只有 Safari 8 支持 tel 类型。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <form action="action_page.php"> 13 Telephone: 14 <input type="tel" name="usrtel"> 15 <input type="submit"> 16 </form> 17 18 <p><b>注释:</b>Safari 8 及更新版本支持 type="tel"。</p> 19 </body> 20 </html>
输出结果:
注释:Safari 8 及更新版本支持 type="tel"。
<input type="url"> 用于应该包含 URL 地址的输入字段。
根据浏览器支持,在提交时可以自动验证 url 字段。
某些智能手机识别 url 类型,并向键盘添加 ".com" 以匹配 url 输入。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <form action="action_page.php"> 13 请添加您的首页: 14 <input type="url" name="homepage"> 15 <input type="submit"> 16 </form> 17 18 <p><b>Note:</b>IE9 及其更早版本不支持 type="url"。</p> 19 </body> 20 </html>
输出结果:
注释:IE9 及其更早版本不支持 type="url"。
method(见本文第2节说明) |
|
规定用于发送 form-data 的 HTTP 方法。 |
提示:表单域须要间name属性才能够把数据提交到服务器(只有不想提交的才不写name属性,如button)。
(1)安全性:
(2)提交大小限制
默认采用的是get提交方式:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <script src="jquery-3.3.1.js"></script> 10 <script type="text/javascript"> 11 </script> 12 13 <style type="text/css"> 14 </style> 15 16 </head> 17 18 <body> 19 <form method="get"> <!--等价于<form>,由于method属性默认为get方式--> 20 Username: <input type="text" name="Name"> 21 Password: <input type="password" name="Pwd"> 22 23 <input type="submit"> 24 </form> 25 </body> 26 </html>
输出结果(当输入用户名和密码,点击提交后,在网页的上方能够看到提交的内容):
输入用户名和密码提交后:
<select> 元素定义下拉列表:
<option> 元素定义待选择的选项。
列表一般会把首个选项显示为被选选项。
您可以经过添加 selected 属性来定义预约义选项。
<form>元素中下拉列表<select>元素的属性:
属性 | 值 | 描述 |
---|---|---|
autofocus⭐ | autofocus | 规定在页面加载后文本区域自动得到焦点。 |
disabled | disabled | 规定禁用该下拉列表。 |
form⭐ | form_id | 规定文本区域所属的一个或多个表单。 |
multiple | multiple | 规定可选择多个选项。 |
name | name | 规定下拉列表的名称。 |
required⭐ | required | 规定文本区域是必填的。 |
size | number | 规定下拉列表中可见选项的数目。 |
<form>元素中下拉列表<select>元素中的<option>属性:
属性 | 值 | 描述 |
---|---|---|
disabled | disabled | 规定此选项应在首次加载时被禁用。 |
label | text | 定义当使用 <optgroup> 时所使用的标注。 |
selected | selected | 规定选项(在首次显示在列表中时)表现为选中状态。 |
value | text | 定义送往服务器的选项值。 |
注释:<option> 标签能够在不带有任何属性的状况下使用,可是一般须要使用 value 属性,此属性会指示出被送往服务器的内容。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 13 <p>您能够经过 selected 属性预选择某些选项。</p> 14 15 <form action="/demo/demo_form.asp"> 16 <select name="cars"> 17 <option value="volvo">Volvo</option> 18 <option value="saab">Saab</option> 19 <option value="fiat">Fiat</option> 20 <option value="audi">Audi</option> 21 </select> 22 <br /><br /> 23 <input type="submit" value="Submit"></input> 24 </form> 25 26 </body> 27 </html>
输出结果:
您能够经过 selected 属性预选择某些选项。
<optgroup>为<option>分组,经过<optgroup>的label属性制定分组的名称:
1 <form> 2 <select name="" id=""> 3 <optgroup label="中国"> 4 <option value="1">上海</option> 5 <option value="2" disabled="disabled">北京</option> <!--置灰,不可选--> 6 <option value="3" selected="selected">深圳</option> <!--默认选中--> 7 </optgroup> 8 9 <optgroup label="外国"> 10 <option value="4">美国</option> 11 <option value="5">英国</option> 12 <option value="6">法国</option> 13 </optgroup> 14 </select> 15 </form>
输出结果:
<textarea>多行文本(多行文本域)
标签订义多行的文本输入,文本区中可容纳无限数量的文本,能够经过cols和rows属性来规定textarea的尺寸,不过最好的办法是使用CSS的height和width属性。textarea中间有空格即认为有内容,就会使得required属性失效(若是配置required属性的话)。
属性 | 值 | 描述 |
---|---|---|
autofocus⭐ | autofocus | 规定在页面加载后文本区域自动得到焦点。 |
cols | number | 规定文本区内的可见宽度。 |
disabled | disabled | 规定禁用该文本区。 |
form⭐ | form_id | 规定文本区域所属的一个或多个表单。 |
maxlength⭐ | number | 规定文本区域的最大字符数。 |
name | name_of_textarea | 规定文本区的名称。 |
placeholder⭐ | text | 规定描述文本区域预期值的简短提示。 |
readonly | readonly | 规定文本区为只读。 |
required⭐ | required | 规定文本区域是必填的。 |
rows | number | 规定文本区内的可见行数。 |
wrap⭐ |
|
规定当在表单中提交时,文本区域中的文本如何换行。 |
提示:能够经过 <textarea> 标签的 wrap 属性设置文本输入区内的换行模式。有关 wrap 属性的详细信息。注释:在文本输入区内的文本行间,用 "%OD%OA" (回车/换行)进行分隔。
<textarea> 元素定义多行输入字段(文本域):
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 13 <p>您能够经过 selected 属性预选择某些选项。</p> 14 15 <form> 16 <!--<textarea></textarea>中间的内容即为textarea中显示的内容,保留空格--> 17 <textarea name="message" rows="10" cols="30"> 18 The cat was playing in the garden. 19 </textarea> 20 21 </form> 22 23 </body> 24 </html>
输出结果:
您能够经过 selected 属性预选择某些选项。
HTML5 增长了以下表单元素:
注释:默认地,浏览器不会显示未知元素。新元素不会破坏您的页面。
举例(经过 <datalist> 设置预约义值的 <input> 元素):
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <!--<meta charset="utf-8">--> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 </head> 10 11 <body> 12 <form action="/demo/demo_form.asp"> 13 <input list="browsers" name="browser"> 14 <datalist id="browsers"> 15 <option value="Internet Explorer"> 16 <option value="Firefox"> 17 <option value="Chrome"> 18 <option value="Opera"> 19 <option value="Safari"> 20 </datalist> 21 <input type="submit"> 22 </form> 23 24 </body> 25 </html>
输出结果:
举例:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <script src="jquery-3.3.1.js"></script> 10 11 </head> 12 13 <body> 14 <form> 15 <table> 16 <tr> 17 <td>用户名:</td> 18 <td><input type="text"></input></td> 19 </tr> 20 <tr> 21 <td>密码:</td> 22 <td><input type="password"></input></td> 23 </tr> 24 <tr> 25 <td>确认密码:</td> 26 <td><input type="password"></input></td> 27 </tr> 28 <tr> 29 <td>请选择城市:</td> 30 <td> 31 <form action=""> 32 <select name="citys"> 33 <option value="Shanghai">上海</option> 34 <option value="Beijing">北京</option> 35 <option value="Shenzhen">深圳</option> 36 <option value="Hefei">合肥</option> 37 </select> 38 </form> 39 </td> 40 </tr> 41 <tr> 42 <td>请选择性别:</td> 43 <td> 44 <!-- <form> --> 45 <label for="male">Male</label> 46 <input type="radio" name="sex" id="male" /> 47 <label for="female">Female</label> 48 <input type="radio" name="sex" id="female" /> 49 <label for="uncertain">Uncertain</label> 50 <input type="radio" name="sex" id="uncertain" /> 51 <!-- </form> --> 52 </td> 53 </tr> 54 <tr> 55 <td>请选择职业:</td> 56 <td> 57 <!-- <form> --> 58 学生<input type="radio" name="career"> 59 教师<input type="radio" name="career"> 60 其余<input type="radio" name="career"> 61 <!-- </form> --> 62 </td> 63 </tr> 64 <tr> 65 <td>请选择爱好:</td> 66 <td> 67 <!-- <form> --> 68 <fieldset> 69 <legend>你的爱好</legend> 70 <label for="basketball">篮球</label> 71 <input type="checkbox" name="hobby" id="basketball"> 72 <label for="run">跑步</label> 73 <input type="checkbox" name="hobby" id="run"> 74 <label for="reading">阅读</label> 75 <input type="checkbox" name="hobby" id="reading"> 76 <label for="surfTheInternet">上网</label> 77 <input type="checkbox" name="hobby" id="surfTheInternet"> 78 </fieldset> 79 <!-- </form> --> 80 </td> 81 </tr> 82 <tr> 83 <td>备注:</td> 84 <td> 85 <!-- <form> --> 86 <input type="textarea" placeholder="这里是备注内容"> 87 <!-- </form> --> 88 </td> 89 </tr> 90 <tr> 91 <td></td> 92 <td> 93 <input type="submit" value="提交"> 94 <input type="reset" value="重置"> 95 </td> 96 </tr> 97 </table> 98 </form> 99 100 </body> 101 </html>
输出结果: