7种form表单提交方式

7种form表单提交方式

无刷新页面提交表单

表单可实现无刷新页面提交,无需页面跳转,如下,通过一个隐藏的iframe实现,form表单的target设置为iframe的name名称,form提交目标位当前页面iframe则不会刷新页面

1
2
3
4
<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

1
2
3
4
<form action="/url.do" method="post">
<input type="text" name="name"/>
<input type="submit" value="提交">
</form>

阻止新页面跳转:从前面可以知道,form 表单的提交会伴随着跳转到action中指定 的url 链接,为了阻止这一行为,可以通过设置一个隐藏的iframe 页面,并将form 的target 属性指向这个iframe,当前页面iframe则不会刷新页面。

1
2
3
4
5
6
<form action="http://localhost:3008/user" method="POST" name="post提交" target="targetIfr">
<p>name: <input type="text" name="username"></p>
<p>password: <input type="password" name="password"></p>
<input type="submit" value="提交">
</form>
<iframe name="targetIfr" style="display:none"></iframe>
1
2
3
4
5
<form action="http://localhost:3008/user" method="GET" name="get提交">
<p>name: <input type="text" name="username"></p>
<p>password: <input type="password" name="password"></p>
<input type="submit" value="提交">
</form>

form 的提交行为需要通过type=submit实现
form 中的method 属性不指定时, form 默认的提交方式为 get请求。
form 表单的提交后会有默认行为,会跳转刷新到action 的页面
form 表单的提交方式,请求头默认的content-type 为 x-www-form-urlencoded
当一个form 表单内部,所有的input 中只有一个 type=’text’ 的时候,enter 键会有默认的提交行为(注意前提条件)。

js提交form表单

js事件触发表单提交,通过button、链接等触发事件,js调用submit()方法提交表单数据,jquery通过submit()方法

1
2
3
<form id="form" action="/url.do" method="post">
<input type="text" name="name"/>
</form>
1
2
js: document.getElementById("form").submit();
jquery: $("#form").submit();

脚本触发form 表单的提交行为

js事件触发表单提交,通过button、链接等触发事件,js调用submit()方法提交表单数据,jquery通过submit()方法

1
2
3
4
5
6
7
8
9
10
11
12
<form action="http://localhost:3008/user" method="POST" name="jsForm" target="targetIfr" id="jsForm">
<p>name: <input type="text" name="username"></p>
<p>password: <input type="password" name="password"></p>
<button id="btn">提交</button>
</form>

<!-- 通过jquery 进行表单的提交 存在问题,并阻止页面跳转刷新-->
<form action="http://localhost:3008/user" method="POST" name="jqueryForm" target="targetIfr" id="jqueryForm">
<p>name: <input type="text" name="username"></p>
<p>password: <input type="password" name="password"></p>
<button id="jqbtn">提交</button>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
// js
var btn = document.getElementById('btn')
var jsForm = document.getElementById('jsForm')
btn.onclick = function () {
jsForm.submit()
}
// jquery
$('#jqbtn').click(function () {
$('#jqueryForm').submit(function () {
console.log('submit success')
return false
})
})

通过脚本提交行为依然存在跳转 新页面刷新的问题
脚本中可以通过阻止默认行为来禁止页面跳转

ajax异步提交表单数据

采用ajax异步方式,通过js获取form中所有input、select等组件的值,将这些值组成Json格式,通过异步的方式与服务器端进行交互,一般将表单数据传送给服务器端,服务器端处理数据并返回结果信息等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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){
}
});

通过ajax 请求模拟form 的提交需要注意 form 表单内 不允许 的存在,否则会和ajax 自身的请求相冲突
ajax 请求中,默认的content-type 为’formdata’,可根据自己的需要修改
后台对不同的content-type 请求头的处理如下:
// 处理请求 的content-type 为application/json
app.use(bodyParser.json())
//处理请求的content-type 为application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}))
ajax 请求需要处理跨域问题,而form 表单的默认提交不需要,原因是,原页面用form 提交到另一个域名之后,原脚本无法获取响应的内容,所以浏览器认为这是安全的,而ajax 需要处理响应的内容,浏览器则认为这是一种跨域的行为
为了解决ajax 的跨域问题,需要在后台的代码中加入跨域的处理
const cors = require(“cors”)
// 解决跨域
app.use(cors())

页面无跳转

如果通过form表单提交请求服务端去下载文件,这时当前页面不会发生跳转,服务端返回void,通过response 去写文件数据,页面会显示下载文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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

1
2
3
4
<form action="/url.do" enctype="multipart/form-data" method="post">
<input type="file" name="name"/>
<input type="submit" value="提交">
</form>
作者

冷冷

发布于

2019-10-09

更新于

2020-10-10

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×