您已经定义了目标,"_blank"
因此请尝试下面的代码并查看示例链接,而不是看到它是如何工作的.. 尝试$.post
不提交,或者您可以使用ajax
将数据保存在后台某处并将其发送到新选项卡..
$(document).ready(function() {
$('#YourForm').submit(function() { // <---- Your form submit event
$(this).target = "_blank"; // <---- to new tab
$.post(url, function(data) {
// whatever you wanna post do it here
});
window.open($(this).prop('action')); // <---- form action attribute = url
return false; // <---- prevents submit
});
});
或使用 ajax
$(document).ready(function() {
$('#YourForm').submit(function() { // <---- Your form submit event
$(this).target = "_blank"; // <---- to new tab
var json = $.toJSON({ yourjsondata1: data1, yourjsondata2: data2 });
$.ajax({
url: "/some/url",
type: "POST",
dataType: "json",
data: json,
contentType: "application/json; charset=utf-8",
success: function (data) {
window.open($(this).prop('action')); // <---- form action attribute = url
}
});
return false; // <---- prevents submit
});
});
看这个例子:
http://jquerybyexample.blogspot.com/2012/05/open-link-in-new-tab-or-new-popup.html
编辑:
$(document).ready(function() {
$('#someid').click(function() {
// lets say you got ur data from your post method
var url = '/api?id=' + $('#someid').val();
$.post(url, function(data) {
if (data != '') {
// update your link
$('#somelink').attr('action', data);
// update target to new tab
$("#somelink").target = '_blank';
// than go
window.open($("#somelink").attr('action'));
return false;
} else {
}
});
});
});
小提琴:http: //jsfiddle.net/BerkerYuceer/nfTWL/