需要用户输入随机数,然后提醒编号为ibnputted的用户

时间:2016-03-18 09:24:51

标签: javascript

请帮忙!看到类似的代码,但不适合我想做的事情。

我需要来自用户的输入,然后比较输入的猜测并提醒用户猜测。

<input type="text" name="Guess" id="Guess" value="" onclick="calculate('Guess', 'output')" />
<input type="button" value="Guess" onclick="calculate('Guess', 'output');" />

function checkGuess(guessStatus) {
    var guessStatus = document.getElementById(guess).value;
    if (guessStatus < ranNum) {
        alert("Your guess is too low" + "<br>");
        document.getElementById("guess").value = '';
    } else if (guessStatus > ranNum) {
        alert("Your guess is too high" + "<br>");
         document.getElementById("guess").value='';
    } else if (guessStatus == ranNum) {
        alert("Winner" + "<br>");
        document.getElementById("guess").disable = true;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个

<input type="text" id="guess" onkeyup="checkGuess(parseInt(this.value))" />
<script>
var ranNum=8;
function checkGuess(guessStatus)
{
      if (guessStatus < ranNum) {
            alert("Your guess is too low" + "<br>");
            document.getElementById("guess").value = '';
        } else if (guessStatus > ranNum) {
            alert("Your guess is too high" + "<br>");
             document.getElementById("guess").value='';
        } else if (guessStatus == ranNum) {
            alert("Winner" + "<br>");
            document.getElementById("guess").disable = true;
        }
}
</script>

答案 1 :(得分:0)

首先:你需要引用:

var guessStatus = document.getElementById("guess").value;

第二: 您没有定义变量:ranNum

Thrid试试这个:

     <!DOCTYPE html>
        <html>
        <body>
        <input type="number" id="guess" value="2">
        <button onclick="checkGuess()">Try it</button>
        <script>



        function checkGuess() {
var min = 0;
var max = 10;
// and the formula is random numbers 0-10:
var ranNum = Math.floor(Math.random() * (max - min + 1)) + min;
            var guessStatus  = document.getElementById("guess").value;
           if (guessStatus < ranNum) {
                alert("Your guess is too low" + "<br>");
                document.getElementById("guess").value = '';
            } else if (guessStatus > ranNum) {
                alert("Your guess is too high" + "<br>");
                 document.getElementById("guess").value='';
            } else if (guessStatus == ranNum) {
                alert("Winner" + "<br>");
                document.getElementById("guess").disable = true;
            }
        }
        </script>

        </body>
        </html>