本文实例讲述了jQuery实现统计输入文字个数的方法。分享给大家供大家参考。具体如下:
jQuery统计输入文字个数可以使用在统计回复字数,标题字数长度提示,增加友好度提高用户体验。代码如下:
复制代码 代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery统计文字个数</title>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<ul>
<li>
<span>新闻标题:</span>
<span><input id="news_title" name="news_title" type="text" size="60" onkeyup="title_len();" onclick="title_len();" maxlength="80"/>
<span id="titlelen">0/80</span>
</li>
</ul>
</body>
<script language="javascript" type="text/javascript">
function title_len(){
var value = $('#news_title').val().length;
if(value == 80){
var string = "<span style=\"color:#FF0000\">"+value+"/80</span>";
}else{
var string = "<span style=\"color:#FF0000\">"+value+"</span>/80";
}
$('#titlelen').html(string);
}
</script>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery统计文字个数</title>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<ul>
<li>
<span>新闻标题:</span>
<span><input id="news_title" name="news_title" type="text" size="60" onkeyup="title_len();" onclick="title_len();" maxlength="80"/>
<span id="titlelen">0/80</span>
</li>
</ul>
</body>
<script language="javascript" type="text/javascript">
function title_len(){
var value = $('#news_title').val().length;
if(value == 80){
var string = "<span style=\"color:#FF0000\">"+value+"/80</span>";
}else{
var string = "<span style=\"color:#FF0000\">"+value+"</span>/80";
}
$('#titlelen').html(string);
}
</script>
</html>
希望本文所述对大家的jQuery程序设计有所帮助。