Jquery实现TextArea 字数限制,并显示当前的输入字符数

来源:互联网 发布:红色名录 数据库 编辑:程序博客网 时间:2024/06/06 08:28
<!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=gb2312" /><title>jquery限制输入字数</title><script type="text/javascript" src="/ajaxjs/jquery1.3.2.js"></script><script type="text/javascript"> $(document).ready(function(){$("#TextArea1").keydown(function(){var curLength=$("#TextArea1").val().length;if(curLength>=5){var num=$("#TextArea1").val().substr(0,4);$("#TextArea1").val(num);alert("超过字数限制,多出的字将被截断!" );}else{$("#textCount").text(4-$("#TextArea1").val().length)}})})</script></head><body><label>剩下<span id="textCount">5</span><br /><textarea name="textarea" id="TextArea1" cols="45" rows="5" >

原创粉丝点击