HTML5表单新增元素与属性

1.表单内元素的form属性

在HTML4中,表单内的从属元素必须书写在表单内部,而在HTML5中,可以把他们书写在页面上任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定的表单了。

代码示例如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9.   
  10. <!--指定这个form的id,这样这个form内部的属性就不一定要写在form内部了,也可以写在外部。只要声明id即可-->  
  11.     <form id="testform">  
  12.         <input type="text">  
  13.         <textarea form="testform"></textarea>  
  14.     </form>  
  15.   
  16. </body>  
  17. </html>  

2.表单内元素的formaction属性

在HTML4中,一个表单内的所有元素只能通过表单的action属性被统一提交到另一个页面,而在HTML5中可以为所有的提交按钮,增加不同的formaction属性,使单击不同的按钮时可以将表单提交到不同的页面。

代码示例如下:

(1)首先在服务端建立一个项目,我在Eclipse中新建一个Dynamic Web Project,然后在三个jsp中编写代码如下:

[html]  view plain  copy
 print ?
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. 这是第一个页面!  
  11. </body>  
  12. </html>  

(2)然后在IDEA中实现HTML代码如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9.     <form>  
  10.         <input type="submit" name="n1" value="提交到第一个页面" formaction="http://localhost:8080/TestHTMLformaction/First.jsp">  
  11.         <input type="submit" name="n2" value="提交到第二个页面" formaction="http://localhost:8080/TestHTMLformaction/Second.jsp">  
  12.         <input type="submit" name="n3" value="提交到第三个页面" formaction="http://localhost:8080/TestHTMLformaction/Third.jsp">  
  13.     </form>  
  14.   
  15. </body>  
  16. </html>  

(3)点击不同的按钮,页面就会跳转到不同的jsp页面上。也就是说,这个在HTML5中,一个form中的不同按钮可以执行不同的formaction,然后跳转到不同的页面。在HTML4中,一个表单只能有一个formaction,就算有多个按钮,也只能跳到同一个formaction指定的页面。


3.表单内元素的formmethod属性

在HTML4中,一个表单内只能有一个action属性用来对表单内所有元素统一指定提交页面,所以每个表单内只有一个method属性来统一指定提交方法。在HTML5中,可以使用formmethod属性来对每一个表单元素分别指定不同的提交方法。formmethod有post和get两种。

代码示例如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9.     <form id="testform">  
  10.         <input type="submit" name="s1" value="v1" formmethod="post" formaction="http://localhost:8080/TestHTMLformaction/First.jsp">使用post提交  
  11.         <input type="submit" name="s2" value="v2" formmethod="get" formaction="http://localhost:8080/TestHTMLformaction/Second.jsp">使用get提交  
  12.     </form>  
  13.   
  14.   
  15. </body>  
  16. </html>  

通过点击不同的按钮,就能进行不同方式的请求,并把数据发送到请求页面上。


4.表单内元素的formenctype属性

在HTML4中,表单元素具有一个enctype属性,该属性用于指定在表单发送到服务器之前应该如何对表单内的数据进行编码。在HTML5中,可以使用formenctype属性对表单分别指定不同的编码方式。

示例代码如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9.     <form>  
  10.   
  11.         <!--表单数据中的空格被转化为加号,但不对表单数据中的特殊字符进行编码-->  
  12.         <input type="text" formenctype="text/plain">  
  13.   
  14.         <!--不对字符进行编码,在使用包含文件上传的表单时必须使用该值-->  
  15.         <input type="text" formenctype="multipart/form-data">  
  16.   
  17.         <!--在发送前编码所有字符,当表单元素的action属性为get时,需要使用这种方式把表单数据转化成字符-->  
  18.         <input type="text" formenctype="application/x-www-form-urlencoded">  
  19.   
  20.   
  21.     </form>  
  22.   
  23. </body>  
  24. </html>  


5.表单内元素的formtarget属性

在HTML4中,表单元素具有一个target属性,该属性用于指定在何处打开表单提交后所需要加载的页面。在HTML5中,可以对多个提交按钮分别使用formtarget属性来指定提交后在何处打开所需加载的页面。

示例代码如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9. <form>  
  10.     <input type="submit" name="n1" value="提交到第一个页面" formtarget="_blank" formaction="http://localhost:8080/TestHTMLformaction/First.jsp">  
  11.     <input type="submit" name="n2" value="提交到第二个页面" formtarget="_self" formaction="http://localhost:8080/TestHTMLformaction/Second.jsp">  
  12.     <input type="submit" name="n3" value="提交到第三个页面" formtarget="_parent" formaction="http://localhost:8080/TestHTMLformaction/Third.jsp">  
  13.     <input type="submit" name="n3" value="提交到第三个页面" formtarget="_top" formaction="http://localhost:8080/TestHTMLformaction/Third.jsp">  
  14.     <input type="submit" name="n3" value="提交到第三个页面" formtarget="framename" formaction="http://localhost:8080/TestHTMLformaction/Third.jsp">  
  15. </form>  
  16.   
  17.   
  18. </body>  
  19. </html>  

6.表单内元素的autofocus属性

为文本框、选择框或按钮控件加上autofocus属性,当画面打开时,该控件自动获得光标焦点。

示例代码如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9.     <form>  
  10.         <input type="text">  
  11.         <input type="text" autofocus>  
  12.   
  13.     </form>  
  14.   
  15. </body>  
  16. </html>  

7.表单内元素的required属性

HTML5中新增的required属性可以应用在大多数输入元素上,在提交时,如果元素中内容为空白,则不允许提交,同时在浏览器中显示提示文字。

示例代码如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.   
  9.     <form>  
  10.   
  11.         <input type="text" required="required">  
  12.         <!--<input type="button" name="we" value="提交" formaction="http://localhost:8080/TestHTMLformaction/First.jsp">-->  
  13.   
  14.         <button formaction="http://localhost:8080/TestHTMLformaction/First.jsp">提交</button>  
  15.   
  16.     </form>  
  17.   
  18. </body>  
  19. </html>  


8.表单内元素的labels属性

在HTML5中,为所有可使用标签的表单元素、button、select元素等,定义一个labels属性,属性值为一个NodeList对象,代表该元素所绑定的标签元素所构成的集合。

示例代码如下:

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8. <!--JS代码和HTML可以混合进行编程-->  
  9.     <script>  
  10.         function Validate(){  
  11.   
  12.             var txtName = document.getElementById("txt_name");  
  13.             var button = document.getElementById("btnValidate");  
  14.             var form = document.getElementById("testform");  
  15.             if(txtName.value.trim() == ""){  
  16.   
  17.                 var label = document.createElement("label");  
  18.                 label.setAttribute("for","txt_name");  
  19.                 form.insertBefore(label,button);  
  20.                 txtName.labels[1].innerHTML = "请输入姓名";  
  21.                 txtName.labels[1].setAttribute("style","font-size:9px;color:red");  
  22.             }  
  23.         }  
  24.     </script>  
  25.   
  26.   
  27.     <form id="testform">  
  28.         <label id="label" for="txt_name">姓名:</label>  
  29.         <input id="txt_name">  
  30.         <input type="button" id="btnValidate" value="验证" onclick="Validate()">  
  31.     </form>  
  32.   
  33. </body>  
  34. </html>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值