表單可實現(xiàn)無刷新頁面提交,無需頁面跳轉(zhuǎn),如下,通過一個隱藏的iframe實現(xiàn),form表單的target設置為iframe的name名稱,
form提交目標位當前頁面iframe則不會刷新頁面
<form action="/url.do" method="post" target="targetIfr"><input type="text" name="name"/></form> <iframe name="targetIfr" style="display:none"></iframe>
一般表單提交通過type=submit實現(xiàn),input type="submit",瀏覽器顯示為button按鈕,通過點擊這個按鈕提交表單數(shù)據(jù)跳轉(zhuǎn)到/url.do
<form action="/url.do" method="post"> <input type="text" name="name"/> <input type="submit" value="提交"></form>
js事件觸發(fā)表單提交,通過button、鏈接等觸發(fā)事件,js調(diào)用submit()方法提交表單數(shù)據(jù),jquery通過submit()方法
<form id="form" action="/url.do" method="post"> <input type="text" name="name"/></form>
js: document.getElementById("form").submit();
jquery: $("#form").submit();
采用ajax異步方式,通過js獲取form中所有input、select等組件的值,將這些值組成Json格式,通過異步的方式與服務器端進行交互,
一般將表單數(shù)據(jù)傳送給服務器端,服務器端處理數(shù)據(jù)并返回結果信息等
<form id="form" method="post"> <input type="text" name="name" id="name"/></form> var params = {"name", $("#name").val()} $.ajax({ type: "POST", url: "/url.do", data: params, dataType : "json", success: function(respMsg){ } });
如果通過form表單提交請求服務端去下載文件,這時當前頁面不會發(fā)生跳轉(zhuǎn),服務端返回void,通過response 去寫文件數(shù)據(jù),
頁面會顯示下載文件。
<form action="/url.do" method="post"> <input type="text" name="name"/> <input type="submit" value="提交"></form>@RequestMapping(value = "/url") public void exportFile(HttpServletRequest req, HttpServletResponse response, String rptId) throws Exception { OutputStream out = null; try { String rptName = "file"; String fileName = new String((rptName + excelAble.getFileSuffix()).getBytes("GBK"), "8859_1"); response.reset(); response.setContentType("application/octec-stream"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); out = response.getOutputStream(); excelAble.exportFile(out); } catch (Exception e) { logger.error(e); } finally { if (out != null) { out.close(); } } }
聯(lián)系客服