用于html中SMS TextArea的Javascript字符计数器

时间:2018-05-24 11:03:19

标签: 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个字符,但之后没有增加。

4 个答案:

答案 0 :(得分:1)

尝试以下代码,如果有帮助

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;
    }

  }

}

答案 1 :(得分:0)

我认为最后if condition是原因。请尝试以下

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

答案 2 :(得分:0)

您可以使用此逻辑

var limit = 160;   // it will be fixed value
var charCount = 170  // it will be dynamic value

var result = Math.ceil(charCount/limit)

现在结果将显示消耗的短信数量。

enter image description here

答案 3 :(得分:0)

您需要计算文本框中的字符总数,并将其除以邮件长度,在您的情况下为160个字符

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