这个问题对我来说似乎很奇怪,我似乎无法解决它。我有一个简单的 HTML 表单;在里面,我确实有一个文本框和一个按钮,如下所示:
<form id="form1" method="get"> <!-- Note: No Action property -->
<div>
<h1>
Simple Test Form</h1>
<table border="0" width="400">
<tr>
<td align="right">
All of these words
</td>
<td>
<input name="txtAll" id="txtAll" type="text" value="testing keyword" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Submit" value="Submit" onclick="myJS(this);" />
</td>
</tr>
</table>
</div>
</form>
MyJS 文件如下:
<script type="text/javascript">
function myJS(which) {
var CONSTANT_SITE_TARGET = "http://servername/redirect/target.aspx?text=";
//capture all values from the form
var allWords = document.getElementById("txtAll").value;
var morestring = ""; //More data manipulation here.....
var url = CONSTANT_SITE_TARGET + allWords + morestring;
window.open(url);
//window.location = url; //Doesn't work
//window.location.href = url; //Doesn't work
//self.location = url; //Doesn't work
//top.location = url; //Doesn't work
}
</script>
如您所见,它不会重定向到 javascript 中指定的 URL。当我使用 window.open 然后它工作。请注意,在
有没有办法重定向它?
最佳答案
不要使用表单标签。或者,将按钮的“类型”属性设置为“按钮”,而不是“提交”。单击按钮时表单会提交,但您不希望这种情况发生。删除表单或更改按钮应该修复不正确的重定向。当您不指定操作时,我很确定默认值为当前 URL。
关于javascript窗口重定向不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10936912/