1. 程式人生 > >js限制字數的輸入

js限制字數的輸入

disable cnblogs tar doc function alt img sub charset

<!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>

技術分享

技術分享

技術分享

js限制字數的輸入