onkeypress="Count('tbxInput');
与你的事件有关系,你按键以后,先相应这个事件onkeypress,此时input的value还没有得到这个值。
等onkeypress响应完了 此时input的value才得到这个字母
与你的事件有关系,你按键以后,先相应这个事件onkeypress,此时input的value还没有得到这个值。
等onkeypress响应完了 此时input的value才得到这个字母
触发事件改一下,改成onkeyup
<html>
<head>
<script type="text/javascript">
function Count(ctrID)
{
var str=document.getElementById(ctrID).value;
document.getElementById("lab1").innerText=str.length;
}
</script>
</head><body>
<input type="text" id="tbxInput" onfocus="Count('tbxInput');" onkeyup="Count('tbxInput');"/>
<br/>
<span id="lab1"/>
</span>
</body>
</html>