2016-06-23 4 views
1

動作しない場合は、私はJavaScriptを使用して、このHTMLページを持っている:Javascriptのページにリダイレクトするとelse文が

<!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をよりよく理解できるように助けてくれる人なら大いに意味します。

+4

'while'ループがちょうど無限ループであるにいくつかの問題があります。実際にはループは必要ないと思います。それを削除し、 'document.getElementById( 'button')のままにしておきます。onclick = websiteBeoordelen;'行。 – Xufox

+2

whileループの 'code'は' undefined'です。 – colecmc

+0

whileループを最初に失います。スクリプト内のonclickにその関数を一度だけバインドします。 – ManoDestra

答えて

0

そのシンプル上記整数にアペンドA + ""コードを次のように変更してください:

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ループを削除するだけで問題なく動作します。

ページが更新されないようにfalseを返します。

+0

'onclick'リスナーは必要ありません。 'while'ループの代わりに' document.getElementById( 'button')。onclick = websiteBeoordelen;という行があれば十分です。 – Xufox

+0

ええ、私はちょうど彼のために新しい道を導入したいと思っています。 – MoustafaS

+1

その後、彼を 'addEventListener'に紹介する方が良いでしょう。 – Xufox

-2

の代わりに ".onclickは=" これを試してやって:

document.getElementById('button').setAttribute('onclick' , 'websiteBeoordelen();"); 

また、値がそうである文字列:

0

はあなたのコード

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. 
関連する問題