1.表单内元素的form属性
在HTML4中,表单内的从属元素必须书写在表单内部,而在HTML5中,可以把他们书写在页面上任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定的表单了。
代码示例如下:
2.表单内元素的formaction属性
在HTML4中,一个表单内的所有元素只能通过表单的action属性被统一提交到另一个页面,而在HTML5中可以为所有的提交按钮,增加不同的formaction属性,使单击不同的按钮时可以将表单提交到不同的页面。
代码示例如下:
(1)首先在服务端建立一个项目,我在Eclipse中新建一个Dynamic Web Project,然后在三个jsp中编写代码如下:
(2)然后在IDEA中实现HTML代码如下:
(3)点击不同的按钮,页面就会跳转到不同的jsp页面上。也就是说,这个在HTML5中,一个form中的不同按钮可以执行不同的formaction,然后跳转到不同的页面。在HTML4中,一个表单只能有一个formaction,就算有多个按钮,也只能跳到同一个formaction指定的页面。
3.表单内元素的formmethod属性
在HTML4中,一个表单内只能有一个action属性用来对表单内所有元素统一指定提交页面,所以每个表单内只有一个method属性来统一指定提交方法。在HTML5中,可以使用formmethod属性来对每一个表单元素分别指定不同的提交方法。formmethod有post和get两种。
代码示例如下:
通过点击不同的按钮,就能进行不同方式的请求,并把数据发送到请求页面上。
4.表单内元素的formenctype属性
在HTML4中,表单元素具有一个enctype属性,该属性用于指定在表单发送到服务器之前应该如何对表单内的数据进行编码。在HTML5中,可以使用formenctype属性对表单分别指定不同的编码方式。
示例代码如下:
5.表单内元素的formtarget属性
在HTML4中,表单元素具有一个target属性,该属性用于指定在何处打开表单提交后所需要加载的页面。在HTML5中,可以对多个提交按钮分别使用formtarget属性来指定提交后在何处打开所需加载的页面。
示例代码如下:
6.表单内元素的autofocus属性
为文本框、选择框或按钮控件加上autofocus属性,当画面打开时,该控件自动获得光标焦点。
示例代码如下:
7.表单内元素的required属性
HTML5中新增的required属性可以应用在大多数输入元素上,在提交时,如果元素中内容为空白,则不允许提交,同时在浏览器中显示提示文字。
示例代码如下:
8.表单内元素的labels属性
在HTML5中,为所有可使用标签的表单元素、button、select元素等,定义一个labels属性,属性值为一个NodeList对象,代表该元素所绑定的标签元素所构成的集合。
示例代码如下: