页面重定向

hod*_*smr 5 html javascript webforms

我正在编写一个脚本,我想要它(现在)根据他们按下的按钮重定向用户.最终它将采取表单输入并将其合并到重定向中,但是现在我只是试图让按钮将用户发送到适当的站点.但是,我的重定向不起作用.

<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>
Run Code Online (Sandbox Code Playgroud)

单击任一按钮时,页面只会重新加载?q =附加到URL,它不会重定向.有帮助吗?

jas*_*bar 1

您想使用 abutton而不是input type='submit'. 您当前的按钮正在提交表单,而不是执行其 onclick 操作。

或者以某种方式阻止提交操作。或者,您可以使用函数将表单操作设置为 url,然后让它提交。