当前位置 : 首页> Web教程 > 如何制作html5新的Input表单

如何制作html5新的Input表单

时间:2016-12-07 15:13   已访问:248次

HTML5表单也是网页制作中重要的组成部分,如果你想在网页中接收浏览者的信息,那么就要给浏览者一个表单,下面我们就根据这些表单输入类型做一个详细的介绍,希望大家可以在这方面学的精学的透。

HTML5 新的 Input 类型

HTML5 拥有多个新的表单输入类型。这些新特性提供了更好的输入控制和验证。

本章全面介绍这些新的输入类型:

color

date

datetime

datetime-local

email

month

number

range

search

tel

time

url

week

注意:并不是所有的主流浏览器都支持新的input类型,不过您已经可以在所有主流的浏览器中使用它们了。即使不被支持,仍然可以显示为常规的文本域。

HTML5 <input> 标签

标签

描述

<input>

描述input输入器

 

Input 类型: color

color 类型用在input字段主要用于选取颜色,如下所示:

OperaSafariChromeFirefoxInternet Explorer

实例

从拾色器中选择一个颜色:

选择你喜欢的颜色: <input type="color" name="favcolor">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  选择你喜欢的颜色: <input type="color" name="favcolor"><br>

  <input type="submit">

</form>

</body>

</html>

运行结果:

选择你喜欢的颜色:

 

 

提交后输出:#000000

Input 类型: date

date 类型允许你从一个日期选择器选择一个日期。

OperaSafariChromeFirefoxInternet Explorer

实例

定义一个时间控制器:

生日: <input type="date" name="bday">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  生日: <input type="date" name="bday">

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:2014-10-29

Input 类型: datetime

datetime 类型允许你选择一个日期(UTC 时间)。

OperaSafariChromeFirefoxInternet Explorer

实例

定义一个日期和时间控制器(本地时间):

生日 (日期和时间): <input type="datetime" name="bdaytime">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  生日 (日期和时间): <input type="datetime" name="bdaytime">

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:2014-10-29

Input 类型: datetime-local

datetime-local 类型允许你选择一个日期和时间 (无时区).

OperaSafariChromeFirefoxInternet Explorer

实例

定义一个日期和时间 (无时区):

生日 (日期和时间): <input type="datetime-local" name="bdaytime">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  生日 (日期和时间): <input type="datetime-local" name="bdaytime">

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:2014-12-24T23:57

Input 类型: email

email 类型用于应该包含 e-mail 地址的输入域。

OperaSafariChromeFirefoxInternet Explorer

实例

在提交表单时,会自动验证 email 域的值是否合法有效:

E-mail: <input type="email" name="email">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  E-mail: <input type="email" name="usremail">

  <input type="submit">

</form>

<p><b>注意:</b> Internet Explorer 9  及更早 IE 版本不支持 type="email" 。</p>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:449173586@qq.com

提示: iPhone 中的 Safari 浏览器支持 email 输入类型,并通过改变触摸屏键盘来配合它(添加 @ 和 .com 选项)。

Input 类型: month

month 类型允许你选择一个月份。

OperaSafariChromeFirefoxInternet Explorer

实例

定义月与年 (无时区):

生日 (月和年): <input type="month" name="bdaymonth">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  生日 ( 月和年 ): <input type="month" name="bdaymonth">

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:2016-12

Input 类型: number

number 类型用于应该包含数值的输入域。

您还能够设定对所接受的数字的限定:

OperaSafariChromeFirefoxInternet Explorer

实例

定义一个数值输入域(限定):

数量 ( 1 到 5 之间 ): <input type="number" name="quantity" min="1" max="5">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  数量 ( 1 到 5 之间): <input type="number" name="quantity" min="1" max="5">

  <input type="submit">

</form>

<p><b>注意:</b>Internet Explorer 9 及更早 IE 版本不支持 type="number" 。</p>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:3

使用下面的属性来规定对数字类型的限定:

属性

描述

disabled

规定输入字段是禁用的

max

规定允许的最大值

maxlength

规定输入字段的最大字符长度

min

规定允许的最小值

pattern

规定用于验证输入字段的模式

readonly

规定输入字段的值无法修改

required

规定输入字段的值是必需的

size

规定输入字段中的可见字符数

step

规定输入字的的合法数字间隔

value

规定输入字段的默认值

 

Input 类型: range

range 类型用于应该包含一定范围内数字值的输入域。

range 类型显示为滑动条。

OperaSafariChromeFirefoxInternet Explorer

实例

定义一个不需要非常精确的数值(类似于滑块控制):

<input type="range" name="points" min="1" max="10">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php" method="get">

Points: <input type="range" name="points" min="1" max="10">

<input type="submit">

</form>

<p><b>注意:</b> Internet Explorer 9 及更早 IE 版本不支持 type="range"。</p>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:7

请使用下面的属性来规定对数字类型的限定:

max - 规定允许的最大值

min - 规定允许的最小值

step - 规定合法的数字间隔

value - 规定默认值

Input 类型: search

search 类型用于搜索域,比如站点搜索或 Google 搜索。

OperaSafariChromeFirefoxInternet Explorer

实例

定义一个搜索字段 (类似站点搜索或者Google搜索):

Search Google: <input type="search" name="googlesearch">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  Search Google: <input type="search" name="googlesearch"><br>

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:IT培训网

Input 类型: tel

OperaSafariChromeFirefoxInternet Explorer

实例

定义输入电话号码字段:

电话号码: <input type="tel" name="usrtel">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  电话号码: <input type="tel" name="usrtel"><br>

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:0371-55025044

Input 类型: time

time 类型允许你选择一个时间。

OperaSafariChromeFirefoxInternet Explorer

实例

定义可输入时间控制器(无时区):

选择时间: <input type="time" name="usr_time">

源代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>it培训网(www.itpxw.cn)</title>

</head>

<body>

<form action="demo-form.php">

  选择时间: <input type="time" name="usr_time">

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:23:57

Input 类型: url

url 类型用于应该包含 URL 地址的输入域。

在提交表单时,会自动验证 url 域的值。

OperaSafariChromeFirefoxInternet Explorer

实例

定义输入URL字段:

添加您的主页: <input type="url" name="homepage">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  添加你的主页: <input type="url" name="homepage"><br>

  <input type="submit">

</form>

<p><b>注意:</b> Internet Explorer 9及更早 IE 版本不支持 type="url" 。</p>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

提交后输出:www.itpxw.cn

提示: iPhone 中的 Safari 浏览器支持 url 输入类型,并通过改变触摸屏键盘来配合它(添加 .com 选项)。

Input 类型: week

week 类型允许你选择周和年。

OperaSafariChromeFirefoxInternet Explorer

实例

定义周和年 (无时区):

选择周: <input type="week" name="week_year">

源代码:

<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>it培训网(www.itpxw.cn)</title> 

</head>

<body>

<form action="demo-form.php">

  选择周: <input type="week" name="year_week">

  <input type="submit">

</form>

</body>

</html>

运行结果:

如何制作html5新的Input表单_www.itpxw.cn

你学会了吗,知道如何制作表单了吗,知道搜索框是如何实现,如何通过网页来获取用户的电话了吗,如果你还没有学会,那就赶紧加入IT培训网,让我们一起学习HTML5中新的Input类型吧!


推荐内容

  • 如何制作HTML多媒体

    科技的进步,促进了多媒体的发展,在生活中我们听到最多的就...

  • 什么是HTML5 SSE

    对于HTML5 SSE方面的知识点你学会了吗,对你学习HTML5有没有帮助...

  • 如何认识HTML5表单属性

    HTML5中新增了哪些表单属性呢,你学会了哪些表单属性呢,如果...

  • 什么是XHTML

    关于XHTML的知识也介绍完了,你学会了吗,知道如何定义一个h...