Home >Web Front-end >H5 Tutorial >HTML5 form properties tutorial example

HTML5 form properties tutorial example

零下一度
零下一度Original
2017-05-16 12:00:232430browse

HTML5 New form attributes

HTML5's 5c73b9aa2fdcb03bf958d2a0e73b3327 and d5fd7aea971a85678ba271703566ebfd tags have added several new attributes.

ff9c23ada1bcecdd1a0fb5d5a0f18437 New attribute:

autocomplete
novalidate

d5fd7aea971a85678ba271703566ebfdNew attribute:

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

ff9c23ada1bcecdd1a0fb5d5a0f18437 / d5fd7aea971a85678ba271703566ebfd 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 ff9c23ada1bcecdd1a0fb5d5a0f18437 tags, as well as the following types of d5fd7aea971a85678ba271703566ebfd 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>

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

ff9c23ada1bcecdd1a0fb5d5a0f18437 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>

d5fd7aea971a85678ba271703566ebfd 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>

d5fd7aea971a85678ba271703566ebfd 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">

d5fd7aea971a85678ba271703566ebfd 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 ff9c23ada1bcecdd1a0fb5d5a0f18437 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>

d5fd7aea971a85678ba271703566ebfd 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>

d5fd7aea971a85678ba271703566ebfd formmethod 属性

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

formmethod 属性覆盖了 ff9c23ada1bcecdd1a0fb5d5a0f18437 元素的的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>

d5fd7aea971a85678ba271703566ebfd formnovalidate 属性

novalidate 属性是一个 boolean 属性.

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

formnovalidate 属性会覆盖 ff9c23ada1bcecdd1a0fb5d5a0f18437 元素的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>

d5fd7aea971a85678ba271703566ebfd formtarget 属性

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

The formtarget 属性覆盖 ff9c23ada1bcecdd1a0fb5d5a0f18437元素的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>

d5fd7aea971a85678ba271703566ebfd height 和 width 属性

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

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

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

实例

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

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

d5fd7aea971a85678ba271703566ebfd list 属性

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

实例

在 fc86e7b705049fc9d4fccc89a2e80ee3中预定义 d5fd7aea971a85678ba271703566ebfd 值:

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

d5fd7aea971a85678ba271703566ebfd min 和 max 属性

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

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

实例

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

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">

d5fd7aea971a85678ba271703566ebfd multiple 属性

multiple 属性是一个 boolean 属性.

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

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

实例

上传多个文件:

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

d5fd7aea971a85678ba271703566ebfd pattern 属性

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

注意:pattern 属性适用于以下类型的 d5fd7aea971a85678ba271703566ebfd 标签: 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">

d5fd7aea971a85678ba271703566ebfd placeholder 属性

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

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

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

实例

input 字段提示文本t:

<input type="text" name="fname" placeholder="First name">

d5fd7aea971a85678ba271703566ebfd required 属性

required 属性是一个 boolean 属性.

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

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

实例

不能为空的input字段:

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

d5fd7aea971a85678ba271703566ebfd 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">

【相关推荐】

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!

Statement:
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