我正在处理一个脚本,我希望它所做的(现在)是基于他们按哪个按钮来重定向用户。最终它将采用表单输入并将其合并到重定向中,但现在我只是试图让按钮将用户发送到适当的站点。但是,我的重定向不起作用。页面重定向
<html>
<head>
<title>
Home
</title>
</head>
<body>
<script type="text/javascript">
<!--
var textstring;
var btnWhichButton;
//Gets the text from the form
function getQ() {
textstring = document.forms['Search'].elements[0].value;
}
//Does a Google Search
function googleSearch() {
window.location ="http://www.google.com";
}
//Does a YouTube Search
function youtubeSearch() {
window.location = "http://youtube.com";
}
//Figure out which button was pressed
function whichButton() {
if (btnWhichButton.value == 'Google Search') {
googleSearch();
} else if (btnWhichButton.value == 'YouTube Search'){
youtubeSearch();
}
}
//main function to run everything
function main() {
getQ();
whichButton();
}
// -->
</script>
<form name="Search" >
<input type="text" name="q" size="31" maxlength="255" value="" />
<input type="submit" value="Google Search" onclick="btnWhichButton=this; main();" />
<input type="submit" value="YouTube Search" onclick="btnWhichButton=this; main();" />
</form>
</body>
</html>
当按下其中一个按钮,页面只是用] Q =重新加载附加到URL,它不重定向。任何帮助?
两件事情,真正提高了我的JS: 1.使用Firebug调试代码 2.使用jQuery实现更努力了相同的结果,并享受浏览器兼容的免费 – DanJ 2010-03-02 07:12:43
附注:如果我更换带警报的window.location('');这样可行。所以我知道onclick动作正在执行。另外,有趣的是,如果我用警报包围重定向,则重定向起作用。 – hodgesmr 2010-03-02 07:16:52
@DanJ不能同意你更多... – 2010-03-02 07:19:11