jquery判断文本域长度
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/l...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>限制多行文本域输入的字符个数</title>
<SCRIPT language=JavaScript>
<!--
var LastCount =0;
function CountStrByte(Message,Total,Used,Remain){ //字节统计
var ByteCount = 0;
var StrValue = Message.value;
var StrLength = Message.value.length;
var MaxValue = Total.value;
if(LastCount != StrLength) { // 在此判断,减少循环次数
for (i=0;i<StrLength;i++){
ByteCount = (StrValue.charCodeAt(i)<=256) ? ByteCount + 1 : ByteCount + 2;
if (ByteCount>MaxValue) {
Message.value = StrValue.substring(0,i);
alert("评论内容最多不能超过 " +MaxValue+ " 个字节!\n注意:一个汉字为两字节。");
ByteCount = MaxValue;
return;
}
}
Used.value = ByteCount;
Remain.value = MaxValue - ByteCount;
LastCount = StrLength;
}
}
//-->
</SCRIPT>
</head>
<body style="font-size:12px">
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
<td align="center" valign="middle"><table width="95%" border="0" cellspacing="0" cellpadding="0">
<form name="form1">
<tr>
<td height="22" align="right" class="font2">评论内容:</td>
<td height="22" align="left"><textarea name="评论内容" cols="45" rows="5" id="评论内容" onKeyDown="CountStrByte(this.form.评论内容,this.form.total,this.form.used,this.form.remain);" onKeyUp="CountStrByte(this.form.评论内容,this.form.total,this.form.used,this.form.remain);"></textarea>
<br />
最多允许
<input name="total" type="text" disabled class="textbox" id="total" value="300" size="3">
个字节 已用字节:
<input name="used" type="text" disabled class="textbox" id="used" value="0" size="3">
剩余字节:
<input name="remain" type="text" disabled class="textbox" id="remain" value="300" size="3">
</td>
</tr>
<tr>
<td height="22"> </td>
<td height="22" align="left"><input name="add" type="submit" class="button" id="add" value="提 交" />
<input name="Submit2" type="reset" class="button" value="重 置" />
</td>
</tr>
</form>
</table></td>
</tr>
<tr>
<td height="100"> </td>
<td height="100"> </td>
</tr>
</table>
</body>
</html> 展开
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>限制多行文本域输入的字符个数</title>
<SCRIPT language=JavaScript>
<!--
var LastCount =0;
function CountStrByte(Message,Total,Used,Remain){ //字节统计
var ByteCount = 0;
var StrValue = Message.value;
var StrLength = Message.value.length;
var MaxValue = Total.value;
if(LastCount != StrLength) { // 在此判断,减少循环次数
for (i=0;i<StrLength;i++){
ByteCount = (StrValue.charCodeAt(i)<=256) ? ByteCount + 1 : ByteCount + 2;
if (ByteCount>MaxValue) {
Message.value = StrValue.substring(0,i);
alert("评论内容最多不能超过 " +MaxValue+ " 个字节!\n注意:一个汉字为两字节。");
ByteCount = MaxValue;
return;
}
}
Used.value = ByteCount;
Remain.value = MaxValue - ByteCount;
LastCount = StrLength;
}
}
//-->
</SCRIPT>
</head>
<body style="font-size:12px">
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
<td align="center" valign="middle"><table width="95%" border="0" cellspacing="0" cellpadding="0">
<form name="form1">
<tr>
<td height="22" align="right" class="font2">评论内容:</td>
<td height="22" align="left"><textarea name="评论内容" cols="45" rows="5" id="评论内容" onKeyDown="CountStrByte(this.form.评论内容,this.form.total,this.form.used,this.form.remain);" onKeyUp="CountStrByte(this.form.评论内容,this.form.total,this.form.used,this.form.remain);"></textarea>
<br />
最多允许
<input name="total" type="text" disabled class="textbox" id="total" value="300" size="3">
个字节 已用字节:
<input name="used" type="text" disabled class="textbox" id="used" value="0" size="3">
剩余字节:
<input name="remain" type="text" disabled class="textbox" id="remain" value="300" size="3">
</td>
</tr>
<tr>
<td height="22"> </td>
<td height="22" align="left"><input name="add" type="submit" class="button" id="add" value="提 交" />
<input name="Submit2" type="reset" class="button" value="重 置" />
</td>
</tr>
</form>
</table></td>
</tr>
<tr>
<td height="100"> </td>
<td height="100"> </td>
</tr>
</table>
</body>
</html> 展开
2个回答
2014-06-21
展开全部
<div id="result">您还可以输入100字</div>
<textarea name="content" id="content" cols="80" rows="5"></textarea><br />
<input type="button" name="btn" value="提交" id="btn" />
<script>
$(document).ready(function(){
$("#content").keyup(function(){
var length = 100;
var content_len = $("#content").val().length;
var in_len = length-content_len;
// 当用户输入的字数大于制定的数时,让提交按钮失效
// 小于制定的字数,就可以提交
if(in_len >=0){
$("#result").html('您还可以输入'+in_len+'字');
$("#btn").attr("disabled",false);
// 可以继续执行其他操作
}else{
$("#result").html('您还可以输入'+in_len+'字');
$("#btn").attr("disabled",true);
return false;
}
});
});
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询