页面重定向 [英] Page Redirection

查看:74
本文介绍了页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a script where all I want it to do (right now) is redirect the user based on which button they press. Eventually it will take form input and incorporate that into the redirect, but right now I'm just trying to get the buttons to send the user off to the appropriate site. However, My redirects aren't working.

<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 =附加到网址,它不会重定向。有什么帮助?

When either button is clicked, the page just reloads with ?q= appended to the url, it doesn't redirect. Any help?

推荐答案

你想使用按钮而不是输入类型='提交'。您当前的按钮正在提交表单,而不是执行他们的onclick操作。

You want to use a button not an input type='submit'. Your current buttons are submitting the form, not performing their onclick actions.

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

Or block the submit action in some way. Or you could use your functions to set the form action to the url and just let it submit.

这篇关于页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆