为什么这段代码没有运行警报(“out of loop”)?它位于功能块内部并且不在for循环中,因此在完成循环后,必须运行下一个警报(“循环”)的状态。所以当两个输入都为真时它为什么不运行(用户:Amir.Nassaji传递:aaaaaaaaaaaa)
<body >
<form onsubmit="return loginCheck()" action= "#"
method="Post">
username:<input type= "text" name= "username" class= "login" /><span class= "validation"></span><br />
password:<input type= "password" name= "password" class= "login"/><span class= "validation"></span><br />
<input type= "submit" value= "submit" />
</form>
<script>
"using strict";
function loginCheck() {
var fault = "";
var ides = ["username" , "password"];
var inputs = document.getElementsByClassName("login");
var inputSpans = document.getElementsByClassName("validation");
var i = 0;
for(i; i<inputs.length ; i++) {
var inputValue = inputs[i].value;
try {
inputSpans[i].innerHTML = "";
if (inputs[i].value === "") {
throw " fild " + ides[i] + " is required.";
}
if (inputs[i].value.length < 8) {
throw ides[i] + " must have at least 8 char length.";
}
if (!inputs[i].value.search(/^\d/i) && i === 0) {
throw ides [i] + " this filed can't start with number";
}
if (inputs[i].value.search(/[~!@#$%^&*\/><":;',()+=-]/i) !== -1 && i === 0) {
throw ides [i] + " there is unaccepted chars in field. ";
}
} catch(err) {
inputSpans[i].innerHTML = "*" + " " + err;
fault = err;
} finally {
alert (i+": finally run " + fault); // this is for testing running finally
if ( i === 1 && fault != null) {
return false;
} else {
continue;
}
}
}
alert ("out of loop");
}
</script>
</body>
答案 0 :(得分:0)
如果您只有一个输入,请参阅小提琴:https://jsfiddle.net/16q2xr8k/11/
触发提醒
如果您有两个输入,请参阅小提琴:https://jsfiddle.net/16q2xr8k/10/
未触发提醒
此条件是说如果多于一个输入返回false
而最后一个alert()
将不会被调用
if ( i === 1 && fault != null) {
return false;
}