天天看点

HTML5表单_表单属性

HTML5新的表单属性

本章讲解涉及 和 元素的新属性。

新的 form 属性:

  • autocomplete
  • novalidate

新的 input 属性:

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

autocomplete属性

autocomplete 属性规定 form 或 input 域应该拥有自动完成功能。

注释:autocomplete 适用于 标签,以及以下类型的 标签:text, search, url, telephone, email, password, datepickers, range 以及 color。

<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get" 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>

<p>请填写并提交此表单,然后重载页面,来查看自动完成功能是如何工作的。</p>
<p>请注意,表单的自动完成功能是打开的,而 e-mail 域是关闭的。</p>

</body>
</html>
           

autofocus属性

autofocus 属性规定在页面加载时,域自动地获得焦点。

<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get">

User name:  <input type="text" name="user_name" autofocus="autofocus" />
            <input type="submit" />

</form>

</body>
</html>
           

form属性

form 属性适用于所有 标签的类型。

form属性必须引用所属表单的 id,如需引用一个以上的表单,请使用空格分隔的列表。

<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get" id="user_form">

First name: <input type="text" name="fname" />
            <input type="submit" />

</form>

<p>下面的输入域在 form 元素之外,但仍然是表单的一部分。</p>

Last name: <input type="text" name="lname" form="user_form" />

</body>
</html>
           

表单重写属性

表单重写属性有:

  • formaction - 重写表单的 action 属性
  • formenctype - 重写表单的 enctype 属性
  • formmethod - 重写表单的 method 属性
  • formnovalidate - 重写表单的 novalidate 属性
  • formtarget - 重写表单的 target 属性

注释:表单重写属性适用于以下类型的 标签:submit 和 image。

<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get" id="user_form">

E-mail: <input type="email" name="userid" /><br />
        <input type="submit" value="Submit" /><br />
        <input type="submit" formaction="http://www.w3school.com.cn/example/html5/demo_admin.asp" value="Submit as admin" /><br />
        <input type="submit" formnovalidate="true" value="Submit without validation" /><br />

</form>

</body>
</html>
           

注释:这些属性对于创建不同的提交按钮很有帮助。

height和width属性

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

<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get">

User name:  <input type="text" name="user_name" /><br />
            <input type="image" src="http://www.w3school.com.cn
/i/eg_submit.jpg" width="99" height="99" />

</form>

</body>
</html>
           

list属性

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

注释:list 属性适用于以下类型的 标签:text, search, url, telephone, email, date pickers, number, range 以及 color。

  • 为一个url框绑定一个可选列表
<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get">

Webpage:    <input type="url" list="url_list" name="link" />
            <datalist id="url_list">
                <option label="W3School" value="http://www.w3school.com.cn" />
                <option label="Google" value="http://www.google.com" />
                <option label="Microsoft" value="http://www.microsoft.com" />
            </datalist>
            <input type="submit" />

</form>

</body>
</html>
           

multiple属性

multiple 属性规定输入域中可选择多个值。

注释:multiple 属性适用于以下类型的 标签:email 和 file。

  • 让用户一次可选择多个文件
<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get">

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

</form>

<p>当您浏览文件时,请试着选择多个文件。</p>

</body>
</html>
           

novalidate属性

novalidate 属性规定在提交表单时不应该验证 form 或 input 域。

注释:novalidate 属性适用于 以及以下类型的 标签:text, search, url, telephone, email, password, date pickers, range 以及 color.

  • 允许用户输入一个非法的email的地址
<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get" novalidate="novalidate">

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

</form>

</body>
</html>
           

pattern属性

模式(pattern) 是正则表达式,规定用于验证 input 域的模式。

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

  • 限定一个文本框只能输入3个字母
<!DOCTYPE HTML>
<html>
<body>

<form action="http://www.w3school.com.cn/example/html5/demo_form.asp" method="get">

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

</form>

</body>
</html>
           

placeholder属性

placeholder属性提供一种提示(hint),描述输入域所期待的值。提示会在输入域为空时显示出现,会在输入域获得焦点时消失:

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

  • 在搜索框中提示用户输入的内容
<!DOCTYPE HTML>
<html>
<body>

<form action="/example/html5/demo_form.asp" method="get">

    <input type="search" name="user_search" placeholder="Search W3School" />
    <input type="submit" />

</form>

</body>
</html>
           

required属性

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

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

  • 限定用户在一个输入框中必须输入内容
<!DOCTYPE HTML>
<html>
<body>

<form action="/example/html5/demo_form.asp" method="get">

Name:   <input type="text" name="usr_name" required="required" />
        <input type="submit" />

</form>

</body>
</html>
           

更多请参考:W3School