Home > Web Front-end > H5 Tutorial > body text

HTML5 form properties tutorial example

零下一度
Release: 2017-05-16 12:00:23
Original
2398 people have browsed it

HTML5 New form attributes

HTML5's <form> and tags have added several new attributes.

New attribute:

autocomplete
novalidate
Copy after login

New attribute:

autocomplete
autofocus
form
formaction
formenctype
formmethod
formnovalidate
formtarget
height and width
list
min and max
multiple
pattern (regexp)
placeholder
required
step
Copy after login

/ autocomplete attribute

autocomplete attribute specifies that the form or input field should have Autocomplete function.

When the user starts typing in an autocomplete field, the browser should display the filled-in options in that field.

Tip: The autocomplete attribute may be turned on in the form element and turned off in the input element.

Note: autocomplete applies to tags, as well as the following types of tags: text, search, url, telephone, email, password, datepickers, range and color.

Example

HTML form 中开启 autocomplete (一个 input 字段关闭 autocomplete ):
<form action="demo-form.php" autocomplete="on">  First name:<input type="text" name="fname"><br>  Last name: <input type="text" name="lname"><br>  E-mail: <input type="email" name="email" autocomplete="off"><br>  <input type="submit"></form>
Copy after login

Tip: In some browsers, you may need to enable the autocomplete function for this attribute to take effect.

novalidate attribute

A boolean attribute of the novalidate attribute.

The novalidate attribute specifies that the form or input field should not be validated when submitting the form.

Example

No need to verify submitted form data

<form action="demo-form.php" novalidate>
  E-mail: <input type="email" name="user_email">
  <input type="submit">
</form>
Copy after login

autofocus attribute

The autofocus attribute is a boolean attribute.

## The #autofocus attribute specifies that a field automatically gains focus when the page loads.

Example

Let the "First name" input input field automatically focus when the page loads:

First name:<input type="text" name="fname" autofocus>
Copy after login

form attribute

form attribute Specifies one or more forms to which the input field belongs.

Tip: If you need to

reference more than one form, use a space-separated list.

Example

The input field located outside the form form references the HTML form (the input form is still part of the form form):

<form action="demo-form.php" id="form1">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
</form>
Last name: <input type="text" name="lname" form="form1">
Copy after login

formaction attribute

The formaction attribute is used to describe the URL address of form submission.

The formaction attribute will override the action attribute in the element.

Note: The formaction attribute is used For type="submit" and type="image".

Example

The following HTMLform form contains two submit

buttons with different addresses:

<form action="demo-form.php">  First name: <input type="text" name="fname"><br>  Last name: <input type="text" name="lname"><br>  <input type="submit" value="Submit"><br>  <input type="submit" formaction="demo-admin.php"  value="Submit as admin"></form>
Copy after login

formenctype attribute

The formenctype attribute describes the data encoding of the form submitted to the server (only for the method="post" form in the form form)

The formenctype attribute overrides the form element's enctype attribute.

Mainly: This attribute is used in conjunction with type="submit" and type="image".

Example

The first submit button has default encoding to send form data, and the second submit button sends form data in "multipart/form-data" encoding format:

<form action="demo-post_enctype.php" method="post">  First name: <input type="text" name="fname"><br>  <input type="submit" value="Submit">  <input type="submit" formenctype="multipart/form-data"  value="Submit as Multipart/form-data"></form>
Copy after login

formmethod 属性

formmethod 属性定义了表单提交的方式。

formmethod 属性覆盖了 元素的的method 属性。

注意: 该属性可以与 type="submit" 和 type="image" 配合使用。

实例

重新定义表单提交方式实例:

<form action="demo-form.php" method="get">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
  <input type="submit" formmethod="post" formaction="demo-post.php"
  value="Submit using POST">
</form>
Copy after login

formnovalidate 属性

novalidate 属性是一个 boolean 属性.

novalidate属性描述了 元素在表单提交时无需被验证。

formnovalidate 属性会覆盖 元素的novalidate属性.

注意: formnovalidate 属性与type="submit一起使用

实例

两个提交按钮的表单(使用与不适用验证 ):

<form action="demo-form.php">  E-mail: <input type="email" name="userid"><br>  <input type="submit" value="Submit"><br>  <input type="submit" formnovalidate value="Submit without validation"></form>
Copy after login

formtarget 属性

formtarget 属性指定一个名称或一个关键字来指明表单提交数据接收后的展示。

The formtarget 属性覆盖 元素的target属性.

注意: formtarget 属性与type="submit" 和 type="image"配合使用.

实例

两个提交按钮的表单, 在不同窗口中显示:

<form action="demo-form.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit as normal">
  <input type="submit" formtarget="_blank"
  value="Submit to a new window">
</form>
Copy after login

height 和 width 属性

height 和 width 属性规定用于 image 类型的 标签的图像高度和宽度。

注意: height 和 width 属性只适用于 image 类型的 标签。

提示:图像通常会同时指定高度和宽度属性。如果图像设置高度和宽度,图像所需的空间 在加载页时会被保留。如果没有这些属性, 浏览器不知道图像的大小,并不能预留 适当的空间。图片在加载过程中会使页面布局效果改变 (尽管图片已加载)。

实例

定义了一个图像提交按钮, 使用了 height 和 width 属性:

<input type="image" src="img_submit.gif" alt="Submit" width="48" height="48">
Copy after login

list 属性

list 属性规定输入域的 datalist。datalist 是输入域的选项列表。

实例

中预定义 值:

<input list="browsers">
<datalist id="browsers">
  <option value="Internet Explorer">
  <option value="Firefox">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>
Copy after login

min 和 max 属性

min、max 和 step 属性用于为包含数字或日期的 input 类型规定限定(约束)。

注意: min、max 和 step 属性适用于以下类型的 标签:date pickers、number 以及 range。

实例

元素最小值与最大值设置:

Enter a date before 1980-01-01:<input type="date" name="bday" max="1979-12-31">Enter a date after 2000-01-01:<input type="date" name="bday" min="2000-01-02">Quantity (between 1 and 5):<input type="number" name="quantity" min="1" max="5">
Copy after login

multiple 属性

multiple 属性是一个 boolean 属性.

multiple 属性规定 元素中可选择多个值。

注意: multiple 属性适用于以下类型的 标签:email 和 file。: email, and file.

实例

上传多个文件:

Select images: <input type="file" name="img" multiple>
Copy after login

pattern 属性

pattern 属性描述了一个正则表达式用于验证 元素的值。

注意:pattern 属性适用于以下类型的 标签: text, search, url, tel, email, 和 password.

提示: 是用来全局 title 属性描述了模式.

提示: 您可以在我们的 JavaScript 教程中学习到有关正则表达式的内容

实例

下面的例子显示了一个只能包含三个字母的文本域(不含数字及特殊字符):

Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code">
Copy after login

placeholder 属性

placeholder 属性提供一种提示(hint),描述输入域所期待的值。

简短的提示在用户输入值前会显示在输入域上。

注意: placeholder 属性适用于以下类型的 标签:text, search, url, telephone, email 以及 password。

实例

input 字段提示文本t:

<input type="text" name="fname" placeholder="First name">
Copy after login

required 属性

required 属性是一个 boolean 属性.

required 属性规定必须在提交之前填写输入域(不能为空)。

注意:required 属性适用于以下类型的 标签:text, search, url, telephone, email, password, date pickers, number, checkbox, radio 以及 file。

实例

不能为空的input字段:

Username: <input type="text" name="usrname" required>
Copy after login

step 属性

step 属性为输入域规定合法的数字间隔。

如果 step="3",则合法的数是 -3,0,3,6 等

提示: step 属性可以与 max 和 min 属性创建一个区域值.

注意: step 属性与以下type类型一起使用: number, range, date, datetime, datetime-local, month, time 和 week.

实例

规定input step步长为3:

<input type="number" name="points" step="3">
Copy after login

【相关推荐】

1. 特别推荐“php程序员工具箱”V0.1版本下载

2. 免费h5在线视频教程

3. php.cn原创html5视频教程

The above is the detailed content of HTML5 form properties tutorial example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template