jQuery监听textarea输入长度

处理发送短信输入框的时候,由于需要计算短信字数,尝试了一些其他方法,没什么效果,所以记录一下一个可用的方法;

这里使用的是JQuery的keyup方法;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="http://static.runoob.com/assets/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#smsContent').keyup(
        function(){
            var count = $(this).val().length;
            $('#textCount').html(count);
        }
    );
});
</script>
<body>
<textarea name="smsContent" id="smsContent" style="width:345px;height:110px;"></textarea>
<div >已录入<span id="textCount">0</span>个字</div>
</body>
</html>


猜你喜欢

转载自blog.csdn.net/weixin_40337982/article/details/79292353