form表單中的 target 屬性,向指定的iframe中提交,實現表單提交不刷新功能


form表單的target,當將iframe設置為隱藏時,可以實現當前頁表單提交而不進行跳轉刷新。代碼如下,首頁在頁面里准備一個form表單和一個iframe。

Java代碼   收藏代碼
  1. <form action="提交的action" method="post" target="myIframe">  
  2. .....................  
  3. </form>  
  4. <iframe name="myIframe" style="display:none"></iframe>  

 

 提交到action后,action返回一串javascript語句。

Js代碼   收藏代碼
  1. String script = "<script>alert('hello!');</script>";  
  2. response.getOutputStream().write(script.getBytes("utf-8"));  

 

alert('hello')將在當前頁執行。原理是form提交后的結果在target指定的iframe里執行,
而iframe已經隱藏了。這樣提交后的效果就和無刷新的效果一樣。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM