js限制字数的输入

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数字限制</title>
</head>

<body>

<textarea id="text" onkeydown="checknum()" onkeyup="checknum()" ></textarea>
<input id="in" type="text" disabled="disabled" />
<script>
    function checknum(){
        var nMax = 10;
        var textDom =  document.getElementById("text");
        var len =textDom.value.length;    
        if(len>nMax){
            textDom.value = textDom.value.substring(0,nMax);
            return;
        }
        document.getElementById("in").value="你还可以输入"+(nMax-len)+"个字";
    }
    checknum();
</script>
</body>
</html>

posted @ 2017-05-10 11:34  Harry-  阅读(7153)  评论(0编辑  收藏  举报