js里面是不是if 跟for不能一起用
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/l...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var arr = new Array("lili", "lucy", "jane");
function check() {
// alert("ddddddd");
var name = document.getElementById("username");
var div = document.getElementById("div");
var namev = name.value;
var str = namev.toString();
var t = str.split(" ");
if (t.length > 1) {
div.innerHTML = "格式不正确";
return;
}
//alert(name.value=" ");
if (name.value = " " || str.trim() =="") {
div.innerHTML = "<font color='red'>用户名不能为空!!</font>"
return;
}
for ( var i = 0; i < arr.length; i++) {
if (arr[i] == namev) {
div.innerHTML = "<font color='green'>用户名可用</font>";
} else {
div.innerHTML = "<font color='red'>用户名不可用</font>";
}
}
}
</script>
</head>
<body>
username:
<input type="text" id="username" onblur="check()" />
<!-- <span id="div"> -->
<div id="div"></div>
</body>
</html> 展开
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var arr = new Array("lili", "lucy", "jane");
function check() {
// alert("ddddddd");
var name = document.getElementById("username");
var div = document.getElementById("div");
var namev = name.value;
var str = namev.toString();
var t = str.split(" ");
if (t.length > 1) {
div.innerHTML = "格式不正确";
return;
}
//alert(name.value=" ");
if (name.value = " " || str.trim() =="") {
div.innerHTML = "<font color='red'>用户名不能为空!!</font>"
return;
}
for ( var i = 0; i < arr.length; i++) {
if (arr[i] == namev) {
div.innerHTML = "<font color='green'>用户名可用</font>";
} else {
div.innerHTML = "<font color='red'>用户名不可用</font>";
}
}
}
</script>
</head>
<body>
username:
<input type="text" id="username" onblur="check()" />
<!-- <span id="div"> -->
<div id="div"></div>
</body>
</html> 展开
2个回答
展开全部
我测试你的代码,程序没有进入for循环,如果你把for循环的位置提到if之前,是可以运行的,问题出在你的if判断上,程序一直显示不正确或者不能为空,然后就退出,不会继续的
t.length > 1 这个可能有问题,因为不管什么值, 它都弹出不正确
我给你修改了一下,测试可行,你看看.
var arr = new Array("lili", "lucy", "jane");
function check() {
// alert("ddddddd");
var name = document.getElementById("username");
var div = document.getElementById("div");
var namev = name.value;
var str = namev.toString();
var t = str.split(" ");
if (t.length > 1) {
div.innerHTML = "格式不正确";
return;
}
//alert(name.value=" ");
if (namev.length == 0) {
div.innerHTML = "<font color='red'>用户名不能为空!!</font>"
return;
}
for ( var i = 0; i < arr.length; i++) {
if (arr[i] == str) {
div.innerHTML = "<font color='green'>用户名可用</font>";
return;
} else {
div.innerHTML = "<font color='red'>用户名不可用</font>";
}
}
}
t.length > 1 这个可能有问题,因为不管什么值, 它都弹出不正确
我给你修改了一下,测试可行,你看看.
var arr = new Array("lili", "lucy", "jane");
function check() {
// alert("ddddddd");
var name = document.getElementById("username");
var div = document.getElementById("div");
var namev = name.value;
var str = namev.toString();
var t = str.split(" ");
if (t.length > 1) {
div.innerHTML = "格式不正确";
return;
}
//alert(name.value=" ");
if (namev.length == 0) {
div.innerHTML = "<font color='red'>用户名不能为空!!</font>"
return;
}
for ( var i = 0; i < arr.length; i++) {
if (arr[i] == str) {
div.innerHTML = "<font color='green'>用户名可用</font>";
return;
} else {
div.innerHTML = "<font color='red'>用户名不可用</font>";
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询