js 判断登录界面的账号密码是否为空
判断登录界面的账号密码是否为空的时候又不想用alert显示就需要用display来隐藏alert啦(在设置时切忌要将隐藏的内容写在账号和密码的div中,否则会根据屏幕的分辨率不同而有所变化,这是本人教训)
首先要定义账号、密码和隐藏部分的id,即varx=document.getElementById("id"),包括按钮的id,然后点击登录按钮的时候(btn.onclick=function(){具体的选择隐藏和显示的内容})要写函数进行选择隐藏和显示;在重置按钮的是(btn.onclick=function(){清空的内容,包括登录按钮显示的内容})
具体代码如下:
<htmlxmlns="http://www.w3.org/1999/xhtml"lang="en">
<head>
<metacharset="UTF-8"/>
<title>请登录</title><basetarget="_blank"/>
<style>
body{
margin:0pxauto;
background-image:url(images/dlbj1.png);
position:relative;
}
.dl{
width:380px;
height:220px;
background-image:url(images/dl.png);
text-align:center;
margin:auto;
margin-top:15%;
position:center;
}
.btn{
font-family:微软雅黑;
font-size:medium;
}
#username{
max-width:200px;
margin:10px0px0px0px;
height:28px;
}
#us{
color:#FFFFFF;
font-family:微软雅黑;
}
#ps{
color:#FFFFFF;
font-family:微软雅黑;
}
#password{
max-width:200px;
margin:20px0px10px0px;
height:28px;
}
</style>
</head>
<body>
<formclass="dl"method="post">
<divclass="dlsy">
<br/><br/><br/>
<divclass="username">
账号:<inputtype="text"id="username"/><spanid="u"style="display:none;color:red;font-size:13px;">*请输入账号!</span>
</div>
<divclass="password">
密码:<inputtype="password"id="password"/><spanid="p"style="display:none;color:red;font-size:13px;">*请输入密码!</span>
</div>
<divclass="empty">
<spanid="up"style="display:none;color:red;font-size:13px;">*账号或密码错误</span>
</div>
<divclass="btn">
<buttontype="button"id="reset"class="btnbtn-defaultbutton-roundedbutton-small">重置</button>
<buttontype="button"id="submit"class="btnbtn-defaultbutton-roundedbutton-small">登录</button>
</div>
</div>
</form>
<scripttype="text/javascript">
varx=document.getElementById("username");
vary=document.getElementById("password");
varbtnSubmit=document.getElementById("submit");
varbtnReset=document.getElementById("reset");
varu=document.getElementById("u");
varp=document.getElementById("p");
varup=document.getElementById("up");
//点击登录按钮时判断账号、密码是否为空或错误
btnSubmit.onclick=function(form){
if(x.value==""){
if(y.value==""){
up.style.display="block";
}
else{
u.style.display="block";
up.style.display="none";
}
}
elseif(y.value==""){
if(x.value==""){
up.style.display="block";
}
else{
p.style.display="block";
up.style.display="none";
}
}
else{
u.style.display="none";
up.style.display="block";
p.style.display="none";
}
}
//点击重置按钮时清空账号密码
btnReset.onclick=function(form){
x.value="";//清空input里的value
y.value="";
u.style.display="none";
up.style.display="none";
p.style.display="none";
}
/*btn.onclick=function(form){
/*if(x!="admin"||y!=123456){
alert('账号或密码错误!');
}
//判断账号密码是否为空则弹出
if(""==x){
alert("请输入账号!");
flag=false;
returnfalse;
}
elseif(""==y){
alert("请输入密码!");
flag=false;
returnfalse;
}
if(flag==true){
flag=true;
}*/
</script>
</body>
</html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!