javaweb限制多行文本域输入字符个数
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DO...
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<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;
break;
}
}
Used.value=ByteCount;
Remain.value=MaxValue-ByteCount;
LastCount=StrLength;
}
}
</script>
</head>
<body>
<textarea name="content" cols="50" rows="9" id="content"
onkeydown="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);" onkeyup="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);">
</textarea>
最多允许
<input name="total" type="text" disabled id="total" value="1600" size="4">
个字节 已用字节:
<input name="used" type="text" disabled id="used" value="" size="4">
剩余字节:
<input name="remain" type="text" disabled id="remain" value="1600" size="4">
</body>
</html>
想不出哪错了··达人们帮帮忙哦 展开
pageEncoding="UTF-8"%>
<!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=UTF-8">
<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;
break;
}
}
Used.value=ByteCount;
Remain.value=MaxValue-ByteCount;
LastCount=StrLength;
}
}
</script>
</head>
<body>
<textarea name="content" cols="50" rows="9" id="content"
onkeydown="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);" onkeyup="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);">
</textarea>
最多允许
<input name="total" type="text" disabled id="total" value="1600" size="4">
个字节 已用字节:
<input name="used" type="text" disabled id="used" value="" size="4">
剩余字节:
<input name="remain" type="text" disabled id="remain" value="1600" size="4">
</body>
</html>
想不出哪错了··达人们帮帮忙哦 展开
展开全部
我运行了你的代码 你的代码没有提交的表单
更改如下:
<form action="test.jsp" method="post"> //test.jsp是随便的一个页面,没有任何内容
<textarea name="content" cols="50" rows="9" id="content"
onkeydown="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);" onkeyup="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);">
</textarea><br><br>
最多允许
<input name="total" type="text" disabled id="total" value="1600" size="4">
个字节 已用字节:
<input name="used" type="text" disabled id="used" value="" size="4">
剩余字节:
<input name="remain" type="text" disabled id="remain" value="1600" size="4">
<br>
<input type="submit" name="submit" value="提交">
</form>
更改如下:
<form action="test.jsp" method="post"> //test.jsp是随便的一个页面,没有任何内容
<textarea name="content" cols="50" rows="9" id="content"
onkeydown="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);" onkeyup="CountStrByte(this.form.content,this.form.total,this.form.used,this.form.remain);">
</textarea><br><br>
最多允许
<input name="total" type="text" disabled id="total" value="1600" size="4">
个字节 已用字节:
<input name="used" type="text" disabled id="used" value="" size="4">
剩余字节:
<input name="remain" type="text" disabled id="remain" value="1600" size="4">
<br>
<input type="submit" name="submit" value="提交">
</form>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询