【问题标题】:Using a 'if' redirect使用“如果”重定向
【发布时间】:2012-05-16 22:51:16
【问题描述】:

我正在尝试使用表单创建一个页面,答案取决于要重定向的页面。如果我使用警报,表单和 javascript 可以 100% 正确工作。我试图用 window.location 命令和其他想法替换警报,但没有任何反应。有人可以解释如何做到这一点,更重要的是你的建议有效吗?

这很好用:

<script type = "text/javascript">
<!--
function checkAnswers()
{
    var myQuiz = document.getElementById( "myQuiz" );
    if ( myQuiz.elements[ 1 ] .checked)
       alert( "Correct");
    else
       alert( "Failed" );
};
</script>

这不起作用!:

<script type = "text/javascript">
<!--
function checkAnswers()
{
    var myQuiz = document.getElementById( "myQuiz" );
    if ( myQuiz.elements[ 1 ] .checked)
        window.location = "www.bing.com";
    else
        window.location = "www.google.com";
};
</script>

【问题讨论】:

  • 在你的网址前加上http://

标签: javascript forms function redirect if-statement


【解决方案1】:

你没有说发生了什么,但你可能遇到了 404 错误,对吧?

如果您的 URL 不以 http:// 开头,它们将被视为相对,浏览器将尝试将您带到一个不存在的页面。使用这个:

function checkAnswers()
{
    var myQuiz = document.getElementById( "myQuiz" );
    if ( myQuiz.elements[ 1 ] .checked)
        window.location = "http://www.bing.com";
    else
        window.location = "http://www.google.com";
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    相关资源
    最近更新 更多