Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有重定向到页面的Javascript if-else语句不起作用_Javascript_Html_If Statement_Url Redirection - Fatal编程技术网

带有重定向到页面的Javascript if-else语句不起作用

带有重定向到页面的Javascript if-else语句不起作用,javascript,html,if-statement,url-redirection,Javascript,Html,If Statement,Url Redirection,我有一个带有javascript的HTML页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type"

我有一个带有javascript的HTML页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Login</title>
<script type="text/javascript">
window.onload = function () {
    function websiteBeoordelen () {
        var code = document.getElementById("code").value;
        if (code == 101101) {
            document.getElementById('button').onclick = function() {
                location.href = "http://google.nl";
            }
        }
        else {
            document.write("The code is wrong, try again!");
        }   
    }
    while (code != 101101) {
        document.getElementById('button').onclick = websiteBeoordelen;
    }
};
</script>
</head>

<body>
<div id="wrap">

<table border="0">
<form method="post">
<label>Code:</label><input name="code" id="code" type="text" class="input"/>
<br /><br />
<label>&nbsp;</label><input name="submit" type="submit" id="button" value="Login" class="button" style="cursor:pointer"/>
</form>
</table>

</div>

</body>
</html>
我想将用户重定向到一个页面,如果他或她有正确的代码。如果代码不正确,我想让它写在同一页的按钮下面,但用户必须能够重试


我知道这可能是一个很容易解决的问题,但我刚刚开始使用Javascript,所以如果你们能帮助我,让我更好地理解Javascript,这将是非常重要的

不要执行.onclick=尝试以下操作:

document.getElementById('button').setAttribute('onclick' , 'websiteBeoordelen();");
该值也是一个字符串,因此:


将+附加到上面的整数

很简单,将代码更改为:

    window.onload = function () {
    function websiteBeoordelen () {
        var code = document.getElementById("code").value;
        if (code == 101101) {
            document.getElementById('button').onclick = function() {
                location.href = "http://google.nl";
            }
        }
        else {
            document.write("The code is wrong, try again!");
        }
      return false;   
    }
   document.getElementById('button').onclick = websiteBeoordelen;
};
只要删除while循环,它就可以工作


添加了return false以防止页面刷新。

代码中存在一些问题

window.onload = function () {//good idea to wrap
    function websiteBeoordelen () {
        var code = document.getElementById("code").value;
        if (code == 101101) {
            document.getElementById('button').onclick = function() {
                //it will (supposedly) work on NEXT click
                location.href = "http://google.nl";
            }
        }
        else {
            //never use document.write. 
            //in this case it adds text inside <head> section
            document.write("The code is wrong, try again!");
        }   
    }
    while (code != 101101) {//no need to create infinite loop
        document.getElementById('button').onclick = websiteBeoordelen;
    }
};

//What works.
window.onload = function () {
   document.getElementById('button').onclick = function() {
     if(document.getElementById("code").value == '101101')
        location.href = "http://google.nl";
     else
        //Create a div or p to show message
        document.getElementById("message").innerHTML = 'The code is wrong, try again!';
     return false;//prevent form submission
   }//.onclick
}//window.onload
//End Of Code.

while循环只是一个无限循环。我想你实际上不需要这个循环。删除它并只保留文档。getElementById'button'。onclick=WebsiteBoordelen;while循环中的line.code未定义。请先丢失该while循环。并将该函数仅绑定到脚本中的onclick一次。onclick侦听器不是必需的。表示document.getElementById'button.onclick=WebsiteBoordelen的行;而不是while循环就足够了。是的,我知道,我只是想给他介绍一种新的方式。然后最好把他介绍给addEventListener。注意,click listener在提交按钮上。挂起的服务器调用将阻止对location.href的更改。甚至没有提到document.write的结果,如果输入的代码不匹配…它仍然不起作用,当我提交错误的代码时,什么也不会发生,页面只是刷新,而且当我提交正确的代码时