javascript提示号码,如果答案错误,继续提示 [英] javascript prompt number and continue prompting if answer is wrong

查看:101
本文介绍了javascript提示号码,如果答案错误,继续提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提示访问者输入1到100之间的整数,然后继续提示,直到输入有效数字。

I need prompt the visitor for an integer between 1 and 100 and to continue prompting until a valid number is entered.

这是我有什么:

<script>

var number = parseInt(prompt("Please enter a number from 1 to 100", ""));

if (number < 100) {
    document.write("Your number (" + number + ") is matches requirements", "");
} else if (isNaN(number)) {
    parseInt(prompt("It is not a number. Please enter a number from 1 to 100", ""));
} else {
    parseInt(prompt("Your number (" + number + ") is above 100. Please enter a number from 1 to 100", ""));
}

</script>

它识别号码,但在号码错误时无法重新询问。你能帮我解释一下你的内容吗?

It recognizes the number but fails to re-ask when the number is wrong. Can you please help me and explain what you added?

非常感谢你。

推荐答案

这样的事情可以解决这个问题:

Something like this should do the trick:

do{
    var selection = parseInt(window.prompt("Please enter a number from 1 to 100", ""), 10);
}while(isNaN(selection) || selection > 100 || selection < 1);

这篇关于javascript提示号码,如果答案错误,继续提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆