用JS控制DIV的显示,在账户或密码输入为空时显示出“请输入正确的用户名和密码”。

我自己编写的下面的代码,但是就算用户名为空也不能显示出隐藏的DIV,这是为什么呢?
<html>
<head>
<title>欢迎来到图书馆借阅系统</title>
<script language="javascript">
function check()
{
var user_name=document.login.user_name.value;
var password=document.login.password.value;
if(user_name==""||password=="")
document.getElementByID('empty').style.display="block";
else
document.getElementByID('empty').style.display="none";
}
</script>
</head>
<body>
<div style="margin-top:200px;">
<center><h2>欢迎来到图书馆借阅系统</h2><br>
<div id="empty" style="display:none;">
<span style="color:red;font-size:10px;">请输入用户名和密码</span>
</div>
<form method=post action="welcome.php" name="login" onsubmit="return check()">
<span>账号:<input type="text" name="user_name" size="20"><br></span>
<span>密码:<input type="password" name="password" size="20"><br></span>
<input type="submit" value="登陆">
</form>

<a href="regist.html"><input type="submit" value="注册" action="regist.html"></a>
</center>
</div>
</body>
</html>

你有2个错误
第一、getElementByID,你写错了,最后那个D是小写,js是区分大小写的,应该是:getElementById
第二、如果用户名或者密码为空的话,那你应该给函数返回一个值,也就是返回假,false
加一个:return false;
这样,你的表单提交事件onSubmit="return check()"运行后才会变成:onSubmit="return false"
这样才能阻止表单的提交追问

我改了大小写和return为什么还是不行呢。如果我用alert弹窗就可以弹出来。

追答

我改了经过测试怎么可以呢?
真不知道你用的什么浏览器
当然,你的代码存在浏览器兼容问题
document.login.user_name.value;
这种语法,只适合在ie或者ie内核并且是兼容方式运行,才会被支持

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-25
楼上说反了,当密码和名字的有一个为空时return false;else里面return true。。我在IE和FF下都测试过了,没问题。
第2个回答  2013-05-25
获取user_name 时 最好用getElementbyId('') 给账号起:<input type="text" id="user_name" size="20"/>
是否显示 应该后 应该加上none 的时候return true blcok时加return false追问

加上了return ture和rutrun false还是不能显示出来啊。。