简体   繁体   English

文本区域长度计算在html中无法正常工作

[英]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: 我的问题是:

  • when i enter 160 characters in the textarea without pressing ENTER button in keyboard it shows correct result(160/160). 当我在文本区域中输入160个字符而不按键盘上的ENTER按钮时,它显示正确的结果(160/160)。
  • But when i use ENTER button 1 time inside the textarea, i can able to enter 159 characters only(159/160). 但是当我在文本区域内使用ENTER按钮1次时,我只能输入159个字符(159/160)。
  • when i use ENTER button 2 times inside the textarea, i can able to enter 158 characters only(158/160). 当我在文本区域内两次使用ENTER按钮时,我只能输入158个字符(158/160)。
  • when i use ENTER button 3 times inside the textarea, i can able to enter 157 characters only(157/160). 当我在文本区域内使用ENTER按钮3次时,我只能输入157个字符(157/160)。
  • and so on.... 等等....

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM