单击按钮后为什么此代码不提供输出?

时间:2017-11-09 09:05:36

标签: javascript

此代码未提供所需的输出。我无法弄清楚为什么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> 

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

试试这个

答案 2 :(得分:0)

将x的值更改为Integer。

喜欢这个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>