form表单提交方式
2018-03-15 17:12
281 查看
无刷新页面提交表单
表单可实现无刷新页面提交,无需页面跳转,如下,通过一个隐藏的iframe实现,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提交
一般表单提交通过type=submit实现,input type="submit",浏览器显示为button按钮,通过点击这个按钮提交表单数据跳转到/url.do<form action="/url.do" method="post"> <input type="text" name="name"/> <input type="submit" value="提交"> </form>
js提交form表单
js事件触发表单提交,通过button、链接等触发事件,js调用submit()方法提交表单数据,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异步提交表单数据
采用ajax异步方式,通过js获取form中所有input、select等组件的值,将这些值组成Json格式,通过异步的方式与服务器端进行交互,一般将表单数据传送给服务器端,服务器端处理数据并返回结果信息等
<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表单提交请求服务端去下载文件,这时当前页面不会发生跳转,服务端返回void,通过response 去写文件数据,页面会显示下载文件。
<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();
}
}
}
form表单上传文件
使用form表单进行上传文件需要为form添加enctype="multipart/form-data" 属性,除此之外还需要将表单的提交方法改成post,如下 method="post", input type的类型需要设置为file
<form action="/url.do" enctype="multipart/form-data" method="post"> <input type="file" name="name"/> <input type="submit" value="提交"> </form>
相关文章推荐
- springmvc-普通form表单提交 4000 方式实现文件上传
- form表单的两种提交方式,submit和button的用法
- ajax请求下载Stream形式的文件,需要使用form表单提交的方式
- ANDROID使用MULTIPARTENTITYBUILDER实现类似FORM表单提交方式的文件上传
- Jquery通过Ajax方式来提交Form表单的具体实现
- jquery下的提交,点击按钮没反应,post方法不执行 JSON方式在FORM表单下不起作用
- form 表单 几种提交方式
- form的onsubmit事件--表单提交前的验证最佳实现方式
- ExtJs中form表单提交的三种方式
- Form表单的二种提交方式(Spring后台controller处理)
- form表单提交的两种方式
- jquery多选择动态参数的使用以及ajax异步请求中提交整个form表单中serializeArray()的使用以及后台值得获取方式
- form表单的提交方式的区别
- 关于防止表单form重复提交的方式
- form表单的input组件的10种提交方式
- form的onsubmit事件--表单提交前的验证最佳实现方式(
- jquery的ajax提交form表单方式总结
- form表单提交后 停留当前界面面 不刷新 的处理方式
- asp.net JS+form表单Post和Get方式提交数据
- 用超链接提交form表单的两种方式