<html> <head> <title>模拟微博输入框的判断</title> <script> function checkValid(){ var value = $("text").value; if (value.length > 140) { $("tips").value = "输入的备注框长度为" + value.length; $("btn").disabled = true; } else { $("tips").value = ""; $("btn").disabled = false; } } function confirm(){ alert("发布"); } function $(_id){ return document.getElementById(_id); } </script> </head> <body> <form name="textForm"> <input type="text" name="tips" id="tips" style="700px;"/><br> <textarea name="content" cols="60" rows="6" id="text" onkeydown="checkValid()"></textarea> <input type="button" onclick="confirm()" value="提交" id="btn"/> </form> </body> </html>
仿照微博输入框写的,当输入的数据超过140个字符时,提示用户,并且设置按钮不可用