当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JS给文本区域(textarea)字数限制

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JS给文本区域(textarea)字数限制


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-30   浏览: 441 ::
收藏到网摘: n/a

今天搭了个“发短信”的页面,找朋友测试,
没想到一位大侠直接弄了本长篇小说发我手机上……
为了我的宝贝手机能继续健康澎湃,给文本区域(textarea)做了一下字数限制:
我们知道,在input标签中可以使用maxlength=”4″属性来直接控制字符数,但是这一招在textarea中行不通。只好写一些JS来做判断:

function textdown(e)
{textevent = e ;
if(textevent.keyCode == 8)
{
return;
}
if(document.getElementById('text').value.length >= 180) 
{
alert("大侠,我手机屏幕小,先输入这么多字好不?") 
if(!document.all)
{
textevent.preventDefault();
}
else
{
textevent.returnValue = false;
}
}
}
function textup()
{
var s = document.getElementById('text').value;
//判断ID为text的文本区域字数是否超过180个
if(s.length > 180) 
{
document.getElementById('text').value=s.substring(0,180);
}
}

附textarea在html中需要调用的参数:

id=”text” onKeyDown=”textdown(event)” onKeyUp=”textup()”