2017-11-09 10 views
1

このコードでは必要な出力が得られません。なぜdocument.getElementById( "demo")= text;ここでは働いていません。ただし、x = document.getElementById( "numb")。value;正しく動作しています。私はこれをチェックした。ボタンをクリックした後にこのコードで出力が得られないのはなぜですか?

<!DOCTYPE html> 
    <html> 
    <body> 
    <p>Please input a number between 0 and 4:</p> 
    <input id="numb"> 
    <button type="button" onclick="check()">Submit</button> 
    <p id="demo"></p> 
    <script> 
    function check() { 
     var text,x; 
     x=document.getElementById("numb").value; 
     switch(x){ 
     case 0: 
     text= "This is Zero"; 
      break; 
     case 1: 
     text = "This is One"; 
      break; 
     case 2: 
     text= "This is Two"; 
      break; 
     case 3: 
      text= "This is Three"; 
      break; 
     case 4: 
      text= "This is Four"; 
      break; 
     default: 
      text="Wrong Input"; 
    }  
    document.getElementById("demo").innerHTML = text;//this gives nothing. 
    } 
    </script> 
    </body> 
    </html> 
+0

のdocument.getElementById( "しびれ")のあなたの価値と同様

。値が入力に入力された文字を返します。 あなたが ケース "0" のための例を切り替える必要があります。 ケース "1": ケース "2": など –

答えて

2

あなたがここにx=document.getElementById("numb").valueを取得している値は、いずれかの

整数、文字列

function check() { 
 
     var text,x; 
 
     x=document.getElementById("numb").value; 
 
     console.log(x); 
 
     switch(x){ 
 
     case '0': 
 
     text= "This is Zero"; 
 
      break; 
 
     case '1': 
 
     text = "This is One"; 
 
      break; 
 
     case '2': 
 
     text= "This is Two"; 
 
      break; 
 
     case '3': 
 
      text= "This is Three"; 
 
      break; 
 
     case '4': 
 
      text= "This is Four"; 
 
      break; 
 
     default: 
 
      text="Wrong Input"; 
 
    }  
 
    document.getElementById("demo").innerHTML = text;//this gives nothing. 
 
    }
<p>Please input a number between 0 and 4:</p> 
 
    <input id="numb"> 
 
    <button type="button" onclick="check()">Submit</button> 
 
    <p id="demo"></p>

または整数としてxの値を解析としてチェックする必要はありませ文字列です

function check() { 
 
     var text,x; 
 
     x=document.getElementById("numb").value; 
 
     x = parseInt(x); 
 
     switch(x){ 
 
     case 0: 
 
     text= "This is Zero"; 
 
      break; 
 
     case 1: 
 
     text = "This is One"; 
 
      break; 
 
     case 2: 
 
     text= "This is Two"; 
 
      break; 
 
     case 3: 
 
      text= "This is Three"; 
 
      break; 
 
     case 4: 
 
      text= "This is Four"; 
 
      break; 
 
     default: 
 
      text="Wrong Input"; 
 
    }  
 
    document.getElementById("demo").innerHTML = text;//this gives nothing. 
 
    }
<p>Please input a number between 0 and 4:</p> 
 
    <input id="numb"> 
 
    <button type="button" onclick="check()">Submit</button> 
 
    <p id="demo"></p>

0
x=parseInt(document.getElementById("numb").value); 

この

0

変更に整数にxの値を試してみてください。このx = parseInt(x)

<!DOCTYPE html> 
 
    <html> 
 
    <body> 
 
    <p>Please input a number between 0 and 4:</p> 
 
    <input id="numb"> 
 
    <button type="button" onclick="check()">Submit</button> 
 
    <p id="demo"></p> 
 
    <script> 
 
    function check() { 
 
     var text,x; 
 
     x=document.getElementById("numb").value; 
 
     alert(x); 
 
     x = parseInt(x); 
 
     switch(x){ 
 
     case 0: 
 
     text= "This is Zero"; 
 
      break; 
 
     case 1: 
 
     text = "This is One"; 
 
      break; 
 
     case 2: 
 
     text= "This is Two"; 
 
      break; 
 
     case 3: 
 
      text= "This is Three"; 
 
      break; 
 
     case 4: 
 
      text= "This is Four"; 
 
      break; 
 
     default: 
 
      text="Wrong Input"; 
 
    }  
 
    document.getElementById("demo").innerHTML = text;//this gives nothing. 
 
    } 
 
    </script> 
 
    </body> 
 
    </html>

+1

なぜdownvote? –

+1

私はそれを削除しました。当初、問題の内容を説明していなかった。 – Xufox

関連する問題