大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索
打印 上一主题 下一主题

JavaScript 限制textarea的输入长度(IE浏览器,Firefox,Chrome)

[复制链接]
跳转到指定楼层
1#
发表于 2009-12-11 11:08:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
复制以下的代码,放进您的html,即可使用

  1. <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">  
  3. <head>  
  4. <head>  
  5. <title> Code:用JavaScript限制textarea输入长度 (For: IE、Firefox ...)</title>  
  6. <meta http-equiv="content-type" content="text/html; charset=gb2312" />  
  7. <meta name="author" content="枫岩,CNLei.y.l@gmail.com">  
  8. <meta name="keywords" content="textarea,输入长度" />  
  9. <meta name="description" content="用JavaScript限制textarea输入长度 (For: IE、Firefox ...)" />  
  10. <style type="text/css" media="all">  
  11. body {font-size:14px;}  
  12. </style>  
  13. </head>  
  14. <body>  
  15. maxlength=10
  16.   
  17. <textarea maxlength="10" onkeyup="return isMaxLen(this)"></textarea>  


  18.   
  19. maxlength=255
  20.   
  21. <textarea maxlength="255" onkeyup="return isMaxLen(this)" onkeydown="return isMaxLen(this)" onchange="return isMaxLen(this)"></textarea>  

  22. <script type="text/javascript">  
  23. function isMaxLen(o){  
  24. var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";  
  25. if(o.getAttribute && o.value.length>nMaxLen){  
  26. o.value=o.value.substring(0,nMaxLen);
  27. alert("The maximum number of characters allowed is 255.");   
  28. }  
  29. }  
  30. </script>  
  31. </body>  
  32. </html>
复制代码

手机版|大马资讯论坛  

GMT+8, 2024-4-19 12:30 , Processed in 0.022631 second(s), 13 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表