【问题标题】:How to display characters remaining for Textbox?如何显示文本框剩余的字符?
【发布时间】:2018-09-19 14:58:44
【问题描述】:

我想在我的代码中实现一个最多允许 150 个字符的简单文本框。那部分相对容易。现在,我如何才能显示我的文本框中还剩下多少个字符?

例子:

<textarea  maxlength="150" rows="5" cols="50" wrap="hard">
    In 150 characters or less, tell me something about yourself...
</textarea>

【问题讨论】:

  • 通过编写JS代码计算已经输入的字符然后从maxlength中减去。
  • 到目前为止你有什么尝试?
  • 还有150 - textarea.value.length 个字符。
  • 这里已经回答了这个问题:count text area

标签: javascript


【解决方案1】:

您可以将input 事件附加到您的textarea,然后每次用户键入或删除一个字符时,您都可以执行 (max - current_characters) 操作并在一个范围内显示结果:

var max = 150;
var textarea = document.querySelector('textarea');
var info = document.querySelector('#info');

//Init the count for the first time
info.textContent = max - textarea.value.length;

textarea.addEventListener('input', function() {
  info.textContent = max - this.value.length;
})
<textarea maxlength="150" rows="5" cols="50" wrap="hard">

In 150 characters or less, tell me something about yourself...

</textarea>
<br> Remaining <span id="info"></span> characteres

【讨论】:

  • 谢谢!我根本不知道如何处理这个问题。我是 JavaScript 新手,所以我真的不知道从哪里开始。你怎么知道使用“.addEventListener”?我什至不知道它的存在。
  • 不客气的兄弟,别担心你只是看了或看了教程视频,当你学习Javascript的时候,你会在事件部分找到addEventListener和所有的事件。
  • 太棒了!再次感谢你!非常感谢您的反馈!
【解决方案2】:

获取框值并将其从您拥有的最大长度中休息:

 let message = document.getElementById('messageBox').value;
 let value =  150;
 var length = value - message;

随意使用长度变量。

这是我前段时间做的一个例子,它甚至可以改变它的颜色,并用正则表达式确定是否有空格:

 // Reads the text area value.
 let message = document.getElementById('messageBox').value;

if((character.length <= 0 || !(/[^\s]/.test(message))) && character.length <= 280){
 // Disables submit button.
 sumbitStatus(true);
 // Sets invisible the message of more than 280 characters.(In case the user select and delete the whole message in one move).
 document.querySelector('.advertisement').style.display =  "none";
 // returns counter to blue color.(In case the user select and delete the whole message in one move).
 document.querySelector('.character').style.color = '#1EAEDB';
 } else if(character.length >= 281){
   // Disables submit button and changes counter to red.
   sumbitStatus(true); 
   document.querySelector('.character').style.color = 'red';
   // Set visible the message of more than 280 characters.
   document.querySelector('.advertisement').style.display = 'block';
  } else{
     // Enables Submit button and returns counter to blue color.
     sumbitStatus(false); 
     if(character.length >= 250){
     document.querySelector('.character').style.color = 'orange';
     // Sets invisible the message of more than 280 characters.
     document.querySelector('.advertisement').style.display = "none";
     }
     else{
     document.querySelector('.character').style.color = '#1EAEDB';
     // Sets invisible the message of more than 280 characters.
     document.querySelector('.advertisement').style.display = "none";
     }
    }  
   }

【讨论】:

  • 你有点“过度回答”。将您的答案保持在最低限度,合乎逻辑的部分。让 OP 处理设计,它会提高可读性。
  • 这个答案让我笑了。不是因为内容,而是方法。你有customersdevelopers。后者通常会在客户改变主意之前尝试思考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多