javascript - 使用 'if' 重定向

标签 javascript forms function redirect if-statement

我正在尝试创建一个带有表单的页面,答案取决于要重定向的页面。如果我使用警报,表单和 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>

最佳答案

您没有说明发生了什么,但您可能会收到 404 错误,对吧?

如果您的网址不以 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";
};

关于javascript - 使用 'if' 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10627848/

相关文章:

javascript - Polymer - 成功发送 core-ajax 参数的问题

javascript - React 如何在输入模糊时触发表单提交?

javascript - 使用 AJAX 将表单数据保存到 PHP

javascript - Meteor 辅助函数与变量?

javascript - Firebug : How to make net tab persistent?

javascript - MeteorJS 计算集合中存在多少相同数据

javascript - 内存和一些有关 JavaScript 的更多信息

javascript - 选择某些值时,如何防止使用 onchange 提交表单的表单提交

c++ - (C++) 这些重载运算符函数有什么区别?

javascript - Json 中的解析函数