[英]Text area length calculation not working properly in html
hi i have a text area in my html page and i am using span tag to show its length 嗨,我的html页面中有一个文本区域,我正在使用span标签显示其长度
html code: html代码:
<textarea cols="50" rows="10" maxlength="160" id="main_text"></textarea><br/>
<span class="charno">0</span>/160
and i am using some jquery to show the length in span tag 我正在使用一些jQuery来显示跨度标签的长度
jquery code: jQuery代码:
$("#main_text").keyup(function(){
$(".charno").html($("#main_text").val().length);
});
My problem is: 我的问题是:
is there any problem in my code or am i doing anything wrong please help......... 我的代码中是否有任何问题,或者我做错了什么,请帮助.........
This is because one enter key is equivalent to 2 alphanumeric keys entered in text area. 这是因为一个Enter键等于在文本区域中输入的2个字母数字键。 If you put "enter key" for 5 times out of 160 characters then you can enter 150 more characters.(150(alphanumeric)+ 10(5*2 for enter key)= 160 characters) 如果您在160个字符中输入5次“输入密钥”,则可以再输入150个字符。(150(字母数字)+ 10(5 * 2输入密钥)= 160个字符)
But while counting the "enter key" using length it will return only one. 但是,在计算使用长度的“输入密钥”时,它只会返回一个。
It should count 1 for enter, if still its taking enter as length 2 please use following trick 输入应为1,如果仍以输入为长度2,请使用以下技巧
$("#main_text").keyup(function(e){
var code = e.keyCode;
if (code == 13)
{
var code = e.keyCode;
var l = $("#main_text").val().length;
$(".charno").html(l-1)
}
$(".charno").html($("#main_text").val().length);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.