计算textarea中的字符 [英] Count characters in textarea
本文介绍了计算textarea中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算文本区域中的字符,所以我刚做了:
I want to count characters in a textarea, so I just made:
<textarea id="field" onkeyup="countChar(this)"></textarea>
function countChar(val){
var len = val.value.length;
if (len >= 500) {
val.value = val.value.substring(0, 500);
} else {
$('#charNum').text(500 - len);
}
};
我的代码有什么问题?这是行不通的! 好吧,那是新手的笔迹,需要帮助.
What's wrong with my piece of code? It does not work! Well, that was a newbie handwriting, need a help.
推荐答案
您在浏览器中看到什么错误?我可以理解,如果您发布的内容不完整,为什么您的代码不起作用,但却不知道我不确定.
What errors are you seeing in the browser? I can understand why your code doesn't work if what you posted was incomplete, but without knowing that I can't know for sure.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script>
function countChar(val) {
var len = val.value.length;
if (len >= 500) {
val.value = val.value.substring(0, 500);
} else {
$('#charNum').text(500 - len);
}
};
</script>
</head>
<body>
<textarea id="field" onkeyup="countChar(this)"></textarea>
<div id="charNum"></div>
</body>
</html>
...对我来说很好.
... works fine for me.
如果超出限制,您可能应该清除charNum div或编写一些内容.
You should probably clear the charNum div, or write something, if they are over the limit.
这篇关于计算textarea中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文