1. $('textarea#txtPrizeNote'); //表示textarea控件名称 2. 'span' 显示剩余字数的标签 HTML: 复制代码 代码如下: style="width: 480px; height: 74px; float: left"> * 剩余字数: 复制代码 代码如下: <BR>//返回val的字节长度 <BR>function getByteLen(val) { <BR>var len = 0; <BR>for (var i = 0; i < val.length; i++) { <BR>if (val[i].match(/[^\x00-\xff]/ig) != null) //全角 <BR>len += 2; <BR>else <BR>len += 1; <BR>} <BR>return len; <BR>} <BR>//返回val在规定字节长度max内的值 <BR>function getByteVal(val, max) { <BR>var returnValue = ''; <BR>var byteValLen = 0; <BR>for (var i = 0; i < val.length; i++) { <BR>if (val[i].match(/[^\x00-\xff]/ig) != null) <BR>byteValLen += 2; <BR>else <BR>byteValLen += 1; <BR>if (byteValLen > max) <BR>break; <BR>returnValue += val[i]; <BR>} <BR>return returnValue; <BR>} <BR>$(function() { <BR>var _area = $('textarea#txtPrizeNote'); <BR>var _info = _area.next(); <BR>var _max = _area.attr('maxlength'); <BR>var _val; <BR>_area.bind('keyup change', function() { //绑定keyup和change事件 <BR>if (_info.find('span').size() < 1) {//避免每次弹起都会插入一条提示信息 <BR>_info.append(_max); <BR>} <BR>_val = $(this).val(); <BR>_cur = getByteLen(_val); <BR>if (_cur == 0) {//当默认值长度为0时,可输入数为默认maxlength值 <BR>_info.text(_max); <BR>} else if (_cur < _max) {//当默认值小于限制数时,可输入数为max-cur <BR>_info.text(_max - _cur); <BR>} else {//当默认值大于等于限制数时 <BR>_info.text(0); <BR>$(this).val(getByteVal(_val,_max)); //截取指定字节长度内的值 <BR>} <BR>}); <BR>}); <BR>