function  getStringLen(Str){   
		var   i,len,code;   
		if(Str==null || Str == "")   return   0;   
		len   =   Str.length;   
		for   (i   =   0;i   <   Str.length;i++)     
		{     
		code   =   Str.charCodeAt(i);   
		if   (code   >   255) {len ++;}   
		}   
		return   len;   
	} 	



//计算当前输入的个数,汉字算两个字符
	function checkLen(value,length){
		var len = getStringLen(value);
		//提示当前输入的个数
		$("#lblSummary"+length).html("当前最大长度为:<font color='red'>"+length+"</font>,还可以输入:<font color='red'>"+(length-len)+"</font>");
		if(len>length){
			flag = false;
		}else{
			flag = true;
		}
		
	}

<textarea class="codeInput" id="summary" title="最长150字符,75个汉字" name="summary" cols="80" rows="5" onkeyup="checkLen(this.value, 150)" wrap="virtual">${notice.summary}</textarea>
						<div id="lblSummary150"></div>

demo可以直接运行:

<!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=gb2312" />
<title>无标题文档</title>
<SCRIPT language=JavaScript> 
function  getStringLen(Str){   
		var   i,len,code;   
		if(Str==null || Str == "")   return   0;   
		len   =   Str.length;   
		for   (i   =   0;i   <   Str.length;i++)     
		{     
		code   =   Str.charCodeAt(i);   
		if   (code   >   255) {len ++;}   
		}   
		return   len;   
	} 	
	
	function checkLen(value,length){
		var len = getStringLen(value);
		
		alert(len);
	}	
</SCRIPT>
</head>

<body onLoad="length()">
<span style="LINE-HEIGHT: 180%">
<textarea name="request" cols="45" rows="8" onKeyUp="checkLen(this.value, 500);">
</textarea>

</body>
</html>


Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐