关于里的字节限制,限制输入小于等于40个字节,超过40个字节键盘不能输入

nihao6154 2013-12-04 05:16:11
我在网上搜索了,有人说用maxlength属性,试了下根本不管用,有说用onpropertychange,但是也不管用,我现在要的需求就是</textarea>里只能输入40个字节,超过40个字节键盘就不能输入了,而且要防止复制粘贴的问题
...全文
353 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rocyao 2013-12-08
  • 打赏
  • 举报
回复
MaxLength="100"
nihao6154 2013-12-05
  • 打赏
  • 举报
回复
咋没几个人呢
zhufeng_toddd 2013-12-05
  • 打赏
  • 举报
回复
向你推荐个网站,应该能帮助你 http://blog.offbeatmammal.com/post/2006/10/26/MaxLength-on-a-Textarea.aspx
eclipsewangping 2013-12-05
  • 打赏
  • 举报
回复
<html> <head> </head> <body> <textarea id="text"></textarea> <label id="byteCount"></label> </body> </html> $( function(){ $('#text').keypress( function(e){ if ($(this).text().length > 50) $(this).text() = $(this).text().substring(0 ,50); } ) } }
龙的传人1988 2013-12-05
  • 打赏
  • 举报
回复
<html> <head> </head> <body> <textarea id="text"></textarea> <label id="byteCount"></label> </body> </html> <script> var text = document.getElementById("text"); var byteCount = document.getElementById("byteCount"); var len = 0; text.onkeyup = function(){ len = getByteCount(text.value); if(len > 40){ text.value = getSubStr(text.value,40); } byteCount.innerText = getByteCount(text.value); } //获取字符串字节数 function getByteCount(str){ if (str == null) { return 0; } else { return (str.length + str.replace(/[\u0000-\u00ff]/g, "").length); } } //根据字节数截取字符串 function getSubStr(str,len){ var tmpLen = 0; for (var i = 0; i < str.length; i++) { var c = str.charAt(i); if (/^[\u0000-\u00ff]$/.test(c)){ tmpLen++; }else { tmpLen += 2; } if(tmpLen > len){ break; } } return str.substring(0,i); } </script>
似梦飞花 2013-12-04
  • 打赏
  • 举报
回复
没检测鼠标事件 自己添加试试
似梦飞花 2013-12-04
  • 打赏
  • 举报
回复
<!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> <script type="text/javascript"> function check(e){ var a=e||window.event; var value=this.value; if(value.length>5){ this.value=value.substring(0,5); } if(value.length==5){ if(a.keyCode!=8){ if(a.preventDefault){ a.preventDefault(); }else{ a.returnValue=false; } } } } window.onload=function(){ var text=document.getElementById("test"); text.onkeydown=check; text.onkeyup=check; text.onkeypress=check; } </script> </head> <body> <textarea id="test"></textarea> </body> </html> 类似这样试试
Deep_Learning 2013-12-04
  • 打赏
  • 举报
回复
html5里面input里面有maxlength这个属性,就可以实现。

87,991

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧