我有一个简单的表格:
<form id="frm" action="process.php" method="post">
<input type="text" id="shortcut" name="shortcut" />
</form>
在
process.php
中:$gomenu = $_POST['shortcut'];
if(strpos($gomenu, "/") === 0) {
//if $gomenu contains `/`, then it should open in new tab
header('Location: newTab.php'); //--> How to open this into a new tab?
} else {
header('Location: somePage.php'); //--> Redirect to somePage.php on the same tab
}
当
shortcut
包含/
的值时,则应将其重定向到新标签页,否则应重定向到同一标签页。怎么做?我知道使用header();
不可能做到这一点,但是我不知道该怎么做。有任何想法吗?谢谢。PS:该表单用于填充菜单代码,然后必须根据
shortcut
上填充的菜单代码重定向页面(如SAP中一样)。但是,如果它包含特定的前缀,则应采用其他方法。更新
在表单上方,我添加了以下脚本:
<script>
$("#frm").ajaxForm({
url: 'process.php', type: 'post'
});
</script>
并在
process.php
上:$gomenu = $_POST['shortcut'];
if(strpos($gomenu, "/") === 0) {
print "<script>
window.open('newTab.php', '_newtab');
</script>";
} else {
header('Location: somePage.php');
}
但是随后它在新的弹出窗口中打开。如何在newtab上打开它?
回答(@fedmich的答案)
$(document).ready(function () {
$('frm#frm').submit(function(){
var open_new_tab = false;
var v = $('#shortcut').val();
if(v.match('/') ){
open_new_tab = true;
}
if ( open_new_tab ) {
$('frm#frm').attr('target', '_blank');
} else {
$('frm#frm').attr('target', '_self');
}
});
});
最佳答案
我认为您可以这样做,默认情况下将目标设置为空白。
<form id="frm" action="process.php" method="post" target="_blank">
</form>
然后在使用表单commit()提交时进行修改,并根据需要导航。
您可以在提交之前使用javascript,更改操作或定位属性
http://jsfiddle.net/fedmich/9kpMU
$('form#frm').submit(function(){
var open_new_tab = false;
var v = $('#shortcut').val();
if(v.match('/') ){
open_new_tab = true;
}
if ( open_new_tab ) {
alert('opening to new tab');
$('form#frm').attr('target', '_blank');
$('form#frm').attr('action', 'http://www.youtube.com');
}
else{
alert('opening to self_page');
$('form#frm').attr('target', '_self');
$('form#frm').attr('action', 'http://www.yahoo.com');
}
});