简体   繁体   English

字符限制显示“负”号中的剩余字符

[英]Character Limit is showing remaining characters in “negative” numbers

I am using tinyMCE version 3, I am using rich-text editor which counts the characters remaining on fly when giving the input. 我正在使用tinyMCE版本3,我正在使用富文本编辑器,该编辑器会在提供输入时计算飞行中剩余的字符。 Since maxLength attribute does not work for tinyMCE3. 由于maxLength属性不适用于tinyMCE3。 I have hardcoded this way but its counting the blank charcters as well 我已经用这种方式进行了硬编码,但同时也计算了空白字符

 < script type = "text/javascript" > tinyMCE.init({ mode: "textareas", theme: "advanced", editor_selector: "mceEditor", theme_advanced_statusbar_location: "bottom", theme_advanced_path: false, statusbar: true, setup: function(editor) { editor.onKeyUp.add(function(c) { // var maxCount = document.getElementById(editor.id).maxLength; var maxCount = 10; console.log(editor.id); var txt = $(editor.getBody()).text(); var txtLen = txt.length; var value = maxCount - (txtLen); $(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value)); if (txtLen > maxCount) { editor.setContent(""); tinyMCE.activeEditor.selection.setContent(txt.substring(0, maxCount)); tinyMCE.activeEditor.focus(); } }); } }); < /script> 
 <textarea id="1" class="mceEditor" cols="100" rows="7" wrap="" maxlength="10">test sample</textarea> 

Counting in negative 负数

Counting in negative numbers and setting content to blank 计算负数并将内容设置为空白

and when I enter 2 characters in between Its removing the last two charcters is there any way I can stop giving the input after it reaches counter "0". 当我在两者之间输入2个字符时,它删除了最后两个字符,在到达计数器“ 0”之后,我有任何办法可以停止提供输入。

I'm not sure I understand your issue fully, as there are multiple things going on here. 我不确定我是否完全理解您的问题,因为这里发生了很多事情。

Showing remaining characters as negative 将其余字符显示为负数

I see that, with your code, if you type 11 characters, the 11th character will be deleted, but then remaining characters shows "-1" instead of "0". 我看到,使用您的代码,如果您键入11个字符,则第11个字符将被删除,但是其余字符显示为“ -1”而不是“ 0”。 Is this your complaint? 这是您的投诉吗? The reason why this is happening is because you trim the text content of the editor down to maxCount, but your remaining chars value was calculated before the trim occurs, so it still has the value of maxCount - untrimmed length. 发生这种情况的原因是因为您将编辑器的文本内容修整为maxCount,但是您的剩余chars值是在修整发生之前计算的,因此它仍然具有maxCount的值-未修剪的长度。 You could easily remedy this in multiple ways, including changing this: 您可以轻松地以多种方式对此进行补救,包括更改此内容:

$(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value));

to this: 对此:

$(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining chars: " + (value > 0 ? value : 0));

Counting Blank Characters 计算空白字符

If you don't want any blank characters included in the count, use this: 如果您不希望在计数中包含任何空白字符,请使用以下命令:

var txtLen = txt.replace(/\s/g,"").length;

If you want to trim just the blank characters off the ends, use this: 如果只想修剪两端的空白字符,请使用以下命令:

var txtLen = txt.trim().length;

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

相关问题 JavaScript不显示剩余字符,但可以在其他页面上使用 - JavaScript not showing remaining characters, but works on other pages 如何限制字符并在php ajax中显示其余字符 - how to limit the characters and show the remaining characters in php ajax Crossfilter在dc.js上显示负数,数据集中没有负数 - Crossfilter showing negative numbers on dc.js with no negative numbers in the dataset 限制用户在React的输入字段的第一个字符中键入数字和字符 - Limit the user from typing numbers and characters in the first character of input field in React JavaScript将文本字段限制为正数和负数 - Javascript limit text field to positive and negative numbers Angular 8 中只有数字的字符限制 - Character limit with just numbers in Angular 8 为什么此javascript使用getTime函数显示负数? - Why is this javascript showing negative numbers with getTime function? Momentjs倒计时-持续时间显示为负数 - Momentjs countdown - Duration is showing negative numbers 字符计数器-退格键不影响剩余字符 - character counter - backspace doesn't reflect on characters remaining 如何验证只接受第一个字母作为字符并在javascript中保留为数字 - how to validate only accept first letter as a character & remaining as a numbers in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM