怎樣在密碼表單已經(jīng)輸入的狀態(tài)下不要彈出confirm對話框
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/> <title>?鼠標經(jīng)過事件?</title> <script?type="text/javascript"> ????function?message(){ ??????var?pw=document.getElementById("pp").value; ??????if(pw==""){ ????????confirm("請輸入密碼后,再單擊確定!") ????????} ????????else{ ????????????alert("成功輸入"+pw) ????????} ????} </script> </head> <body> <form> 密碼:<input?name="password"?type="password"?id="pp"?> <input?name="確定"?type="button"?value="確定"? onclick="message()"/> </form> </body> </html>
為什么我的判定條件是if(pw==null)的時候,不好用呢??
2018-01-01
因為pw==“ ”判斷的是表單內(nèi)容是否為空,而null是指不存在pw這個對象。
當你沒有定義pw這個對象的時候,你用pw==null返回值為true。
2018-01-01
在<input type="text"/>中未規(guī)定value的初始值同時用戶未輸入時,value的默認值是""。