I have this problem with counting element's value length in textarea.

I tried both DOM and jQuery, but it seems like .length is the problem. I alerted $('#' + field1).val().length and it seems that it starts counting from 0 even though there is 1 symbol in a field.

BTW $('#' + field1).val().length + 1 doesn't help, it even makes it worse.

Here the JSFiddle with my code.

有帮助吗?

解决方案

Change the event from onkeypress to onkeyup. The problem is that the javascript is counting the characters before the character has actually been entered into the textarea.

Working fiddle: http://jsfiddle.net/YbUf4/8/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top