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

为什么我这个在input中输入值的时候,console都不显示值?

为什么我这个在input中输入值的时候,console都不显示值?

19990000 2017-10-05 11:51:22
<!DOCTYPE html> <html> <head>    <meta charset="UTF-8">    <title>表单验证</title>    <style>        .title{            font-weight: bold;            font-size:18px;        }        .names{            width:358px;            height:42px;            margin-left:38px;            -webkit-border-radius:5px;            -moz-border-radius:5px;            border-radius:5px;        }        .btn{            width:78px;            height:42px;            margin-left:15px;            background: rgb(46,122,184);            color: #fff;            font-size:18px;            font-weight: bold;            outline: none;            border:0;            border-radius: 5px;        }        .prompt{            margin-left:78px;            font-size:16px;            color: #ccc;        }    </style> </head> <body>    <form action="#">        <p>            <label for="names">名称</label>            <input type="text" id="names" name="user_name" value="">            <button type="submit">验证</button>        </p>        <p>必填,长度为4~16个字符</p>    </form> </body> <script>    var names=document.getElementById("names").value;    console.log(names); </script> </html>
查看完整描述

2 回答

?
我爱钱端

TA贡献14条经验 获得超3个赞

因为初始化的时候值是空的,输入的时候打印新的值需要键盘事件,可以写成这样

var input = document.getElementById("names");

input.onkeyup = function (){
   var names=document.getElementById("names").value;
   console.log(names)
}

第一次回答问题,代码也很粗糙。

查看完整回答
反对 回复 2017-10-05
?
慕前端7352391

TA贡献3条经验 获得超0个赞

你的value不是已经初始化为空了吗

查看完整回答
反对 回复 2017-10-05
  • 2 回答
  • 0 关注
  • 2427 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信