日期:2014-05-16  浏览次数:20632 次

HTML5(八).HTML5 表单属性
HTML5 表单属性


本章讲解涉及 <form> 和 <input> 元素的新属性。
新的 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 适用于 <form> 标签,以及以下类型的 <input> 标签:text, search, url, telephone, email, password, datepickers, range 以及 color。

当用户在自动完成域中开始输入时,浏览器应该在该域中显示填写的选项:
<form action="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>

注释:在某些浏览器中,您可能需要启用自动完成功能,以使该属性生效。

autofocus 属性

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

注释:autofocus 属性适用于所有 <input> 标签的类型。
User name: <input type="text" name="user_name"  autofocus="autofocus" />


form 属性

form 属性规定输入域所属的一个或多个表单。

注释:form 属性适用于所有 <input> 标签的类型。

form 属性必须引用所属表单的 id:
<form action="demo_form.asp" method="get" id="user_form">
First name:<input type="text" name="fname" />
<input type="submit" />
</form>
Last name: <input type="text" name="lname" form="user_form" />

注释:如需引用一个以上的表单,请使用空格分隔的列表。

表单重写属性

表单重写属性(form override attributes)允许您重写 form 元素的某些属性设定。

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

注释:表单重写属性适用于以下类型的 <input> 标签:submit 和 image。
注释:这些属性对于创建不同的提交按钮很有帮助。

height 和 width 属性

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

注释:height 和 width 属性只适用于 image 类型的 <input> 标签。
<input type="image" src="img_submit.gif" width="99" height="99" />


list 属性

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

注释:list 属性适用于以下类型的 <input> 标签:text, search, url, telephone, email, date pickers, number, range 以及 color。
Webpage: <input type="url" list="url_list" name="link" />
<datalist id="url_list">
<option label="3wschools" value="http://www.3wschool.com.cn" />
<option label="Google" value="http://www.google.com" />
<option label="Microsoft" value="http://www.microsoft.com" />
</datalist>


min、max 和 step 属性

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

max 属性规定输入域所允许的最大值。

min 属性规定输入域所允许的最小值。

step 属性为输入域规定合法的数字间隔(如果 step="3",则合法的数是 -3,0,3,