简体   繁体   English

按Tab,空格,删除和退格键时如何计算字符?

[英]How to count characters when pressing tab, space,delete and backspace?

I know this question may be a duplicate but I wasn't able to find a solution so far. 我知道这个问题可能是重复的,但到目前为止我还找不到解决方案。 Maybe the way I searched is wrong. 也许我搜索的方式是错误的。

I need to count the total characters of textarea along with spaces using "space key" "tab key" "delete" or "backspace". 我需要使用“空格键”,“选项卡键”,“删除”或“退格键”来计算textarea的总字符以及空格。

So far I have the below code but I don't know why it is not considering tab space in the count. 到目前为止,我有下面的代码,但我不知道为什么它不考虑计数中的制表符空间。

<textarea id="myTextArae"></textarea>
$("#myTextArae").on("keydown", function(e) {
  if (e.keyCode === 9) { // tab was pressed
    let start = this.selectionStart;
    let end = this.selectionEnd;
    var $this = $(this);
    $this.val($this.val().substring(0, start) + "\t" + $this.val().substring(end));
    this.selectionStart = this.selectionEnd = start + 1;
    var txt = $("#balanceCharacInTxtarea").val();
    if (txt != parseInt("0")) {
      e.preventDefault();
    }
    return false;
  }
})

$("#myTextArea").on("paste", function (e) {
  setTimeout(function () {
    that.check(e, e1, 0);
  }, 100);
});

check(eventcode, e1, code) {
  if (code === 0) {
    this.calculate = this.characterCount(e, e1, default);
  } if (code === 1) {
    this.calculate = e1 ? e1.length : 0;
  }
}
var default = 175

How about something as easy as 像这样简单的事情怎么样

 $("#yourtextarea").keyup(function() {
    var count = $("#yourtextarea").val().length;
 });

This will count spaces, tabs, newlines and other characters. 这将计算空格,制表符,换行符和其他字符。
It will trigger on paste as well. 它也会在粘贴时触发。

Example : 范例

 $(document).ready(function(){ $("#test").html(0); //this is for counting the characters $("#message").keyup(function() { var txtlgt = $("#message").val().length; $("#test").html(txtlgt); }); //This is for enabling tabs in the textarea $("#message").on("keydown", function(e) { if (e.keyCode === 9){ let start = this.selectionStart; let end = this.selectionEnd; var $this = $(this); $this.val($this.val().substring(0, start) + "\\t" + $this.val().substring(end)); this.selectionStart = this.selectionEnd = start + 1; return false; } }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test"></div> <textarea id="message"></textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下退格键时删除一组字符(Javascript) - Delete a group of characters when backspace is pressed (Javascript) 如何检测何时要删除的字符为“零宽度空间”并执行两次delete或Backspace命令? - How can I detect when character to be deleted is Zero Width Space and execute delete or backspace command twice? 用户选择文本并按Delete / Backspace时未触发Word Count的AngularJS指令 - AngularJS directive for Word Count not triggered on user selecting text and pressing delete/backspace Contenteditable:如何在按del或退格时完全删除跨度 - Contenteditable: How to completely remove span when pressing del or backspace 按下退格键或删除键时如何执行操作 - How to do something when backspace or delete is pressed 按退格键时禁用向后导航,允许在字段中退格 - Disable backward navigation when pressing backspace, allow backspace in fields 按退格或删除按钮时如何删除整个超链接? - How to delete entire hyperlink when backspace or delete button is pressed? 只允许空格、字母、退格和制表符 - Allow only white space, letters, backspace, and tab 在文本框上按删除或退格键时删除已删除的字符或文本 - Getting deleted character or text on pressing delete or backspace on a textbox Javascript:在可编辑的div中按下回车键时如何触发退格键? - Javascript: how to trigger a backspace keypress when pressing enter key in conteditable div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM