为了账号安全,请及时绑定邮箱和手机立即绑定

为什么不能有条件地弹窗

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 鼠标经过事件 </title>
<script type="text/javascript">
    function message(){
    passwd=document.getElementById("password").value;
    if(passwd=="")
      {confirm("请输入密码后,再单击确定!");} }
</script>
</head>
<body>
<form>
密码:<input name="password" type="password" id="password">
<input name="确定" type="button" value="确定" onmouseover="message()"/>
</form>
</body>
</html>

http://img1.sycdn.imooc.com//5785ed450001a04705600344.jpg

我想让他先判断,如果输入框为空再弹窗,否则不弹窗,但是没有效果,执行上面的代码不弹窗。

正在回答

7 回答

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title> 鼠标经过事件 </title>
    <script type="text/javascript">
        function message(){
             var passwd = document.getElementById("password").value;
            console.log(passwd);
            if(passwd ===""){
                confirm("请输入密码后,再单击确定!");
            }
        }
    </script>
</head>
<body>
<form>
    密码:<input name="password" type="password" id="password">
    <input name="确定" type="button" value="确定" onclick="message()"/>
</form>
</body>
</html>


0 回复 有任何疑惑可以回复我~

<!DOCTYPE HTML>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title> 鼠标经过事件 </title>
   <script type="text/javascript">
       function message(){
            var passwd = document.getElementById("password").value;
           if(passwd ==""){
               
           }else {
               confirm("请输入密码后,再单击确定!");
           }
       }
   </script>
</head>
<body>
<form>
   密码:<input name="password" type="password" id="password">
   <input name="确定" type="button" value="确定" onmouseover="message()"/>
</form>
</body>
</html>

0 回复 有任何疑惑可以回复我~

怕段语句的错误,null不是空值的意思,而是没有赋值。应改为“”即可。

0 回复 有任何疑惑可以回复我~

http://www.imooc.com/qadetail/134356

0 回复 有任何疑惑可以回复我~

你们说得都不对,我在别的章节中找到答案了,只需要把null换成“”就可以了。

3 回复 有任何疑惑可以回复我~
#1

慕仰3473226

null 和 "" 有什么区别呀
2016-07-18 回复 有任何疑惑可以回复我~
#2

CharlesLvm 提问者 回复 慕仰3473226

var str1 = null; str引用为空。 var str2 = ""; str引用一个空串。 也就是null没有分配空间,""分配了空间,因此str1还不是一个实例化的对象,而str2已经实例化。
2016-07-19 回复 有任何疑惑可以回复我~
passwd.value才是值,,你可以输出一下看一看,你获取到的是一个对象,不是值。。。
0 回复 有任何疑惑可以回复我~

在js中你并没有获取button按钮,

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
JavaScript进阶篇
  • 参与学习       468299    人
  • 解答问题       21893    个

本课程从如何插入JS代码开始,带您进入网页动态交互世界

进入课程

为什么不能有条件地弹窗

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信