警报对话框无法关闭

时间:2017-01-19 07:27:03

标签: javascript jquery

我在Google Chrome版本54.0.2840.100(64位)中运行了以下代码。它会显示一个连续询问输入的警告对话框,直到用户输入正确答案,但问题是当我单击警告对话框中的关闭按钮或取消按钮而不是关闭时,它会继续要求输入。此外,关闭按钮标签是不可点击的。但是我可以关闭chrome主窗口但是有一个代码来纠正这个。

var answer = Number(Math.floor(Math.random()*10));

do{
    var number = Number(prompt("Guess a number"));
    if(number-answer >10){
        alert("Too big!!");
    }
    else if(number-answer < 10 && number>answer ){
        alert("It's bigger, but you are close!");
    }
    else if(answer-number < 10 && number<answer){
        alert("It's smaller, but you are close!");
    }
    else if(answer-number > 10){
        alert("Too small!!");
    }
    else if(number==answer){
    alert("You WIN !!");
    break;
    }
}while(number!=answer);

4 个答案:

答案 0 :(得分:2)

最简单直接的答案是删除do...while循环。 &#39;因为在你输入正确的答案之前会保持代码运行。

因此,您可以检查输入的数字,如果是空值,则将其转换为您的数字并运行循环。

第一手检查null可以很容易地检查0作为输入

var answer = Number(Math.floor(Math.random()*10));

do{
    var number = prompt("Guess a number");
    if(number!=null){
        number = Number(number);
    } else {
        break;
    }
    if(number-answer >10){
        alert("Too big!!");
    }
    else if(number-answer < 10 && number>answer ){
        alert("It's bigger, but you are close!");
    }
    else if(answer-number < 10 && number<answer){
        alert("It's smaller, but you are close!");
    }
    else if(answer-number > 10){
        alert("Too small!!");
    }
    else if(number==answer){
    alert("You WIN !!");
    break;
    }
}while(number!=answer);

答案 1 :(得分:1)

var answer = Number(Math.floor(Math.random()*10));

do{
    var number = Number(prompt("Guess a number"));
    if(number === 0){ // this check is needed
        break;
    }

    if(number-answer >10){
        alert("Too big!!");
    }
    else if(number-answer < 10 && number>answer ){
        alert("It's bigger, but you are close!");
    }
    else if(answer-number < 10 && number<answer){
        alert("It's smaller, but you are close!");
    }
    else if(answer-number > 10){
        alert("Too small!!");
    }
    else if(number==answer){
    alert("You WIN !!");
    break;
    }
}while(number!=answer);

关闭提示时,返回null

将null传递给Number函数将返回0

因此添加了检查0

您还可以按如下方式修改代码,以允许0作为用户的输入

var input= prompt("Guess a number");
if(input === null){
  break;
}

var number = Number(input);
    ... rest of the code

答案 2 :(得分:1)

如果点击cancel,则输入值为null,然后Number(null)将为0。除非猜测数为0,否则警告对话框不会被关闭。

在投射之前,最好检查输入值是否为null

Window.prompt()

答案 3 :(得分:0)

我添加了一行代码来检查用户是否取消了提示。

var answer = Number(Math.floor(Math.random()*10));

do{
    var number = Number(prompt("Guess a number"));

    // Add the below code to check for null and exit

    if (number === null)break;

    // END

    if(number-answer >10){
        alert("Too big!!");
    }
    else if(number-answer < 10 && number>answer ){
        alert("It's bigger, but you are close!");
    }
    else if(answer-number < 10 && number<answer){
        alert("It's smaller, but you are close!");
    }
    else if(answer-number > 10){
        alert("Too small!!");
    }
    else if(number==answer){
    alert("You WIN !!");
    break;
    }
}while(number!=answer);