<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<input id="txt" type="text"/>
输入<span id='info'></span>字数,有效字符:<span id='info_txt'></span>
<script type="text/javascript">
$(function(){
var tmp_len = 0;
var tmp_str = '';
var input_reg = /[\u4e00-\u9fa5]/g;
$("#txt").mousemove(function(){
var input_str = $(this).val().match(input_reg);
//如果匹配到了中文
if(input_str!=null){
//将匹配到的数组转成字符串
input_str = input_str.join('');
}
var input_len = $(this).val().length;
if (input_len != tmp_len && input_str != tmp_len){
//设置文本款文字
$(this).val(input_str);
$("#info").text(input_len);
$("#info_txt").text(input_str);
}
tmp_len = input_len;
tmp_len = input_str;
});
});
</script>