html 中 SMS TextArea 的 Javascript 字符计数器

标签 javascript html count character textarea

我设计了短信发送页面来发送短信。我使用 Javascript 显示从 160 开始倒数的字符计数。问题是我不知道如何计算每 160 个字符后的短信数量。

我的js函数代码:

function CountCharIndividual(txtIndividualMessage) {
        var count = document.getElementById(txtIndividualMessage).value.length;
        if (count > 1 && count <= 160) {
            document.getElementById('charstatus2').innerHTML = 160 - document.getElementById(txtIndividualMessage).value.length;
        }
        if (document.getElementById(txtIndividualMessage).value.length > 160 - 1) {
            document.getElementById('charstatus2').innerHTML = "Maximum characters reached & now " + "showing new count: " + document.getElementById(txtIndividualMessage).value.length;
        }
        if (document.getElementById(txtIndividualMessage).value.length >= 160) {
            document.getElementById('smsCount').innerHTML = 1;
        }
    }

它显示了 smsCount 的 innerHTML 中的前 160 个字符,但随后不再增加。

最佳答案

尝试下面的代码,如果有帮助

function CountCharIndividual(txtIndividualMessage) {

  var count = document.getElementById(txtIndividualMessage).value.length;
  var maxLength = 160;
  var smsCount = parseInt(count / maxLength);
  if (smsCount > 0) {
    document.getElementById('smsCount').innerHTML = smsCount;
  } else {
    if (count > (maxLength - 1)) {
      document.getElementById('charstatus2').innerHTML = "Maximum characters reached & now " + "showing new count: " + count;
    } else {
      document.getElementById('charstatus2').innerHTML = maxLength - count;
    }

  }

}

关于html 中 SMS TextArea 的 Javascript 字符计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50507709/

相关文章:

neo4j - 是否可以使用 neo4j 的默认图表?

SQL Join 和 Count 不能正确 GROUP BY?

php mysql 计数

javascript - 30 秒后显示 Div

html - 定位旋转inline-block伪元素

mongodb:查询数组的计数/大小

javascript - 将图像从数据库对齐到 html 页面

javascript - JavaScript 文件的变色龙模板?

javascript - 在node.js中将数组对象转换为json

jQuery hide() 效果不佳