慕斯慕斯
2017-12-30 22:51
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 鼠标经过事件 </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)的时候,不好用呢??
因为pw==“ ”判断的是表单内容是否为空,而null是指不存在pw这个对象。
当你没有定义pw这个对象的时候,你用pw==null返回值为true。
在<input type="text"/>中未规定value的初始值同时用户未输入时,value的默认值是""。
JavaScript进阶篇
469250 学习 · 22584 问题
相似问题