我一直在努力让它发挥作用。我有基本代码,但每次我尝试添加最后一个要求时,我都会破坏代码。我确定我错过了很多,这就是为什么我不能添加带有显示计数的警报框。因此,它询问用户名称,他们想要查看的次数和警告框,如果他们输入无效答案,他们会看到警告框告诉他们输入正确的值。我坚持的最后一部分是用户输入的值决定了多少个警告框显示他们的名字,而盒子需要说出它是哪个盒子。因此,<Name>
这是<count of displays>
的时间数<total time to display>
。我正在使用限制和FOR尝试不同的事情,用户Y变量是限制器。任何线索或帮助将不胜感激。
<!DOCTYPE html>
<html>
<body>
<p>Please enter your name followed by how many times you would like to be alerted.</p>
<button onclick="myFunction()">Start</button>
<script>
function myFunction() {
var x;
var name = prompt("Please enter your name", "");
if (name == null || name == "") {
alert("Please input a name.");
return false;
}
else {
var y;
var y = prompt("Please enter a number between 1-10");
if (y == null || y == "") {
alert("Please input a number for the times to alert the name.");
return false;
}
if (y > 10) {
alert("Please input a number between 1 and 10.")
var y = prompt("Please enter a number between 1-10");
}
if (y <= 0) {
alert("Please input a number great than zero.")
var y = prompt("Please enter a number between 1-10");
}
}
}
</script>
</body>
</html>
答案 0 :(得分:1)
没什么可改变的。
尝试:
function myFunction() {
var x, y;
var name = prompt("Please enter your name", "");
if (name == null || name == "") {
alert("Please input a name.");
return false;
}
else {
var y = prompt("Please enter a number between 1-10");
if (y == null || y == "") {
alert("Please input a number for the times to alert the name.");
return false;
}
while (y >= 10 || y <= 0) {
alert("Please input a number between 1 and 10.")
var y = prompt("Please enter a number between 1-10");
if (y == null || y == "") {
alert("Please input a number for the times to alert the name.");
return false;
}
}
}
for(var i = 0; i < y; i++) {
alert("this is an alert");
}
}
这在循环中检查1&lt; = y&lt; = 10。
答案 1 :(得分:1)
这会删除大部分alert
语句并保留prompt
,直到提交有效值。
function myFunction() {
var x,y,name = "";
while(name.length < 1) {
name = prompt("Please enter your name", "");
}
while(!((y > 0) && (y <11))) {
y = prompt("Please enter a number between 1-10");
}
for(x = 0; x < y; x++) {
alert(name);
}
}
工作示例: