admin管理员组文章数量:1327945
I am trying to create a blog ment form
with a textarea
and a span
which shows to the user the number of remaining characters that can be introduced in the text area.
So I have this form:
<form action="ment.php" method="POST" accept-charset="utf-8">
<textarea name="ment" id="ment" rows="4" cols="56"></textarea>
<input type="submit" value="Post" />
<span id="ment-chars-left">512</span> characters left
</form>
And then I wrote the following jQuery code:
$('#ment')
.keydown(function(event) {
$('#ment-chars-left').html('' + (512 - $textarea.val().length));
});
The problem is that when typing .keydown
is called first, which prints the number of remaining characters and then the new character typed is shown in the textarea
. So the number of remaining characters does not have a correct value, being bigger by one unit. To make this work .keydown
should be called after the insertion of the new character.
How can I resolve this problem?
I am trying to create a blog ment form
with a textarea
and a span
which shows to the user the number of remaining characters that can be introduced in the text area.
So I have this form:
<form action="ment.php" method="POST" accept-charset="utf-8">
<textarea name="ment" id="ment" rows="4" cols="56"></textarea>
<input type="submit" value="Post" />
<span id="ment-chars-left">512</span> characters left
</form>
And then I wrote the following jQuery code:
$('#ment')
.keydown(function(event) {
$('#ment-chars-left').html('' + (512 - $textarea.val().length));
});
The problem is that when typing .keydown
is called first, which prints the number of remaining characters and then the new character typed is shown in the textarea
. So the number of remaining characters does not have a correct value, being bigger by one unit. To make this work .keydown
should be called after the insertion of the new character.
How can I resolve this problem?
Share Improve this question asked Oct 6, 2011 at 11:35 Calin-Andrei BurloiuCalin-Andrei Burloiu 1,4812 gold badges14 silver badges25 bronze badges 2- Not actually answering your question, but watch out for new lines, which can be one character or two depending on the browser and how the new line was added. – Alohci Commented Oct 6, 2011 at 12:08
-
You are right in UNIX-like operating systems a new line is
\n
, but in Windows it is\r\n
. – Calin-Andrei Burloiu Commented Oct 25, 2011 at 13:57
5 Answers
Reset to default 5Use keyup()
instead.
You will also want to bind a few other events.
Use bind('keyup paste drop')
.
keyup
for event when key is released, paste
if someone pastes text in your textarea
, and drop
if someone drops a chunk of text in your textarea
.
jsFiddle.
you could use $('#ment').keyup()
:)
Why not use .keyup()
?
$('#ment')
.keyup(function(event) {
$('#ment-chars-left').html('' + (512 - $textarea.val().length));
});
try the .keyup
event instead
Use the keypress
event instead, which happens when a key produces a character, not when the key is pressed. (For example the shift key causes a keydown
event but not a keypress
event, and a key that repeats causes keypress
events for each character, but only a keydown
event at the first one.)
Use the setTimeout
method to start a timer that will run the code after the event has been handled:
$('#ment')
.keypress(function() {
window.setTimeout(function(){
$('#ment-chars-left').html('' + (512 - $textarea.val().length));
}, 0);
});
版权声明:本文标题:javascript - Show in real time the number of characters in a HTML textarea while the user types - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742222016a2435503.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论