问题描述
我正在尝试输入输入您的密码:提示,直到输入正确的密码。密码输入的尝试没有限制。到目前为止,我使用Javascript提出了这个代码
I'm trying to make "Enter your Password: " prompt appear until the correct password is entered. There is no limit to the attempts of password entry. So far I came up with this code using Javascript
function promptPassword() {
var name = prompt("Enter your Username: ");
var pwd = prompt("Enter your Password: ");
if (pwd == 'P@ssw0rd') {
alert("Password is correct, you are allowed to enter the site");
}
while (pwd != 'P@ssw0rd') {
alert("Login is incorrect");
prompt("Enter your Password: ");
}
}
<body onload="promptPassword();">
</body>
当我首先输入正确的密码时尝试正确的警报显示,当我点击确定时它会消失,这是预期的。
当我输入错误的密码时,它会一直提示再次输入密码,这也是预期的。
但这里的问题是当我第一次输入错误的密码然后在下次尝试时输入正确的密码时,它仍然会提示再次输入密码。输入正确的密码后,密码正确,您可以进入网站警报应显示,并且当点击确定时应该消失
When I enter the correct password in first attempt the correct alert displays and it goes away when I click 'ok', which is expected.When I type wrong passwords it keeps prompting to enter the password again, which is also as expected.But the problem here is when I first enter wrong password and then enter right password in the next attempts, it still keeps prompting to enter the password again. Once the correct password is entered, "Password is correct, you are allowed to enter the site" alert should display and should go away, when 'ok' is clicked
推荐答案
您的提示返回值未在while循环中分配。试试这个:
Your prompt return value isn't being assigned in the while loop. Try this:
function promptPassword( )
{
var name = prompt ("Enter your Username: ");
var pwd = prompt ("Enter your Password: ");
while (pwd != 'P@ssw0rd'){
alert("Login is incorrect");
pwd = prompt ("Enter your Password: ");
}
alert("Password is correct, you are allowed to enter the site");
}
这篇关于尝试显示提示,直到使用Javascript输入正确的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!