我正在尝试重定向到简单表单提交的谷歌搜索,但它无法正常工作。这是我的代码剥离:
<script type="text/javascript">
function search(){
alert("in function!");
var a = document.getElementById("searchbox").value;
a = "http://www.google.com/#q=" + a;
a = a.replace(" ","+");
alert(a);
document.location = a;
}
</script>
<form method="post" action="#" onSubmit="search()">
<input type="text" id="searchbox" />
</form>
我根本没有收到警报。
答案 0 :(得分:2)
两件事,首先它适用于我,http://jsfiddle.net/LzwAt/1/ 确保你在文件的head标签中有javascript,并且不要忘记提交按钮;)。
此外,如果您只想提交给谷歌,您可以随时做:
<form action="http://google.com/search" method="GET">
<input type="text" name="q" />
<input type="submit" name="go" value="Search">
</form>
编辑: 试试这个:
<html>
<head>
<script type="text/javascript">
function search()
{
alert("in function!");
var a = document.getElementById("searchbox").value;
a = "http://www.google.com/#q=" + a;
a = a.replace(" ","+");
alert(a);
document.location = a;
}
</script>
</head>
<body>
<form method="post" action="#" onSubmit="search();return false;">
<input type="text" id="searchbox" />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>