我想使用javascript prompt((方法实现3次尝试登录表单。它显示您已留下2次尝试,并对下一次尝试进行ddnt检查。如何解决这个问题?这是我的脚本
<script type="text/javascript">
var attempt = 3;
var userName=prompt("Username","");
var passWord=prompt("Password","");
if (userName+passWord=="john2541798432" || userName+passWord=="tim3591798752")
{
alert('success');
}
else
{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
if( attempt == 0)
{
alert('you are blocked');
}
}
</script>
<script type="text/javascript">
var loggedin = false;
var attempt = 3;
while(!loggedin && attempt > 0){
var userName=prompt("Username","");
var passWord=prompt("Password","");
if (userName+passWord=="john2541798432" || userName+passWord=="tim3591798752")
{
loggedin = true;
alert('success');
}
else
{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
if( attempt == 0)
{
alert('you are blocked');
}
}
}
</script>
根据您的脚本,我认为您可以添加一个隐藏的输入字段并更新隐藏字段的值,以验证最大尝试次数。请参阅以下脚本
<input type="hidden" id="attempt" value="3">
<script type="text/javascript">
while(1)
{
var userName=prompt("Username","");
if(userName != "")
var passWord=prompt("Password","");
var attempt = document.getElementById("attempt").value;
if(userName != "" && passWord != "")
{
if (userName+passWord=="john2541798432" || userName+passWord=="tim3591798752")
{
alert('success');
break;
}
else
{
document.getElementById("attempt").value = attempt -1;
alert("You have left "+(attempt-1)+" attempt;");
attempt = attempt - 1;
if( attempt == 0)
{
alert('you are blocked');
break;
}
}
}
}
</script>