js 判斷登錄界面的賬號密碼是否為空
判斷登錄界面的賬號密碼是否為空的時候又不想用alert顯示就需要用display來隱藏alert啦(在設置時切忌要將隱藏的內(nèi)容寫在賬號和密碼的div中,否則會根據(jù)屏幕的分辨率不同而有所變化,這是本人教訓)
首先要定義賬號、密碼和隱藏部分的id,即var x=document.getElementById("id"),包括按鈕的id,然后點擊登錄按鈕的時候(btn.onclick=function(){具體的選擇隱藏和顯示的內(nèi)容})要寫函數(shù)進行選擇隱藏和顯示;在重置按鈕的是(btn.onclick=function(){清空的內(nèi)容,包括登錄按鈕顯示的內(nèi)容})
具體代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta charset="UTF-8" /> <title>請登錄</title><base target="_blank" /> <style> body{ margin:0px auto; 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: 10px 0px 0px 0px ; height: 28px ; } #us{ color: #FFFFFF ; font-family : 微軟雅黑; } #ps{ color: #FFFFFF ; font-family : 微軟雅黑; } #password{ max-width: 200px ; margin: 20px 0px 10px 0px ; height: 28px ; } </style> </head> <body> <form class="dl" method="post"> <div class="dlsy"> <br/><br/><br/> <div class="username"> 賬號:<input type="text" id="username"/><span id="u" style="display: none;color: red;font-size:13px;">*請輸入賬號!</span> </div> <div class="password"> 密碼:<input type="password" id="password"/><span id="p" style="display: none;color: red;font-size:13px;">*請輸入密碼!</span> </div> <div class="empty"> <span id="up" style="display: none;color: red;font-size:13px;">*賬號或密碼錯誤</span> </div> <div class="btn"> <button type="button" id="reset" class="btn btn-default button-rounded button-small">重置</button> <button type="button" id="submit" class="btn btn-default button-rounded button-small">登錄</button> </div> </div> </form> <script type="text/javascript"> var x=document.getElementById("username"); var y=document.getElementById("password"); var btnSubmit = document.getElementById("submit"); var btnReset = document.getElementById("reset"); var u=document.getElementById("u"); var p=document.getElementById("p"); var up=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"; } } else if(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; return false; } else if(""==y){ alert("請輸入密碼!"); flag=false; return false; } if(flag==true){ flag=true; }*/ </script> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
JavaScript中Async/Await通過同步的方式實現(xiàn)異步的方法介紹
在JavaScript的異步編程中,我們經(jīng)常使用回調(diào)函數(shù)、Promise和 Async/Await來解決異步操作的問題,Async/Await 又是Promise的語法糖,它的出現(xiàn)讓異步編程變得更加直觀和易于理解,本文將詳細講解Async/Await如何通過同步的方式實現(xiàn)異步2023-06-06JavaScript數(shù)字數(shù)組的13個實用小技巧
數(shù)組是JS最常見的一種數(shù)據(jù)結(jié)構(gòu),咱們在開發(fā)中也經(jīng)常用到,在這篇文章中,提供一些小技巧,幫助咱們提高開發(fā)效率,這篇文章主要給大家分享介紹了關于JavaScript數(shù)字數(shù)組的13個實用小技巧,需要的朋友可以參考下2023-11-11JavaScript獲取XML數(shù)據(jù)附示例截圖
這篇文章主要介紹了JavaScript獲取XML數(shù)據(jù)的方法,需要的朋友可以參考下2014-03-03JS 兩日期相減,獲得天數(shù)的小例子(兼容IE,FF)
這篇文章介紹了兩日期相減,獲得天數(shù)的小例子,有需要的朋友可以參考一下2013-07-07微信小程序?qū)崿F(xiàn)的涂鴉功能示例【附源碼下載】
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)的涂鴉功能,涉及微信小程序事件響應及畫筆的相關操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-01-01