2014-09-25 56 views
-4

如何使用jquery創建n個短信計數?SMS字符數

像這個代碼只有3短信計數。我需要使用像這個相同的代碼N個短信計數。 怎麼辦?

$('#smsText').smsArea(); 

的HTML:

<b id="smsCount"></b> SMS (<b id="smsLength"></b>) Characters left 
<textarea id="smsText"></textarea> 

(function($){ 
    $.fn.smsArea = function(options){ 

    var 
    e = this, 
    cutStrLength = 0, 

    s = $.extend({ 

     cut: true, 
     maxSmsNum: 3, 
     interval: 400, 

     counters: { 
      message: $('#smsCount'), 
      character: $('#smsLength') 
     }, 

     lengths: { 
      ascii: [160, 306, 459], 
      unicode: [70, 134, 201] 
     } 
    }, options); 


    e.keyup(function(){ 

     clearTimeout(this.timeout); 
     this.timeout = setTimeout(function(){ 
    var 
      smsType, 
      smsLength = 0, 
      smsCount = -1, 
      charsLeft = 0, 
      text = e.val(), 
      isUnicode = false; 

      for(var charPos = 0; charPos < text.length; charPos++){ 
       switch(text[charPos]){ 
        case "\n": 
        case "[": 
        case "]": 
        case "\\": 
        case "^": 
        case "{": 
        case "}": 
        case "|": 
        case "€": 
         smsLength += 2; 
        break; 

        default: 
         smsLength += 1; 
       } 


       if(text.charCodeAt(charPos) > 127 && text[charPos] != "€") isUnicode = true; 
      } 

      if(isUnicode){ 
       smsType = s.lengths.unicode; 

      }else{ 
       smsType = s.lengths.ascii; 
      } 

      for(var sCount = 0; sCount < s.maxSmsNum; sCount++){ 

       cutStrLength = smsType[sCount]; 
       if(smsLength <= smsType[sCount]){ 

        smsCount = sCount + 1; 
        charsLeft = smsType[sCount] - smsLength; 
        break 
       } 
      } 

      if(s.cut) e.val(text.substring(0, cutStrLength)); 
      smsCount == -1 && (smsCount = s.maxSmsNum, charsLeft = 0); 

      s.counters.message.html(smsCount); 
      s.counters.character.html(charsLeft); 

     }, s.interval) 
    }).keyup() 
}}(jQuery)); 

我試過短信字符數。但是這個代碼只有3個數字。我需要n個短信計數。就像創建超過100或200個短信一樣。如果有人知道手段發送查詢。

+1

您必須向我們展示您嘗試過的方式以幫助您解決問題。 – avcajaraville 2014-09-25 12:10:41

回答

0

在這段代碼中,筆者給陣列,最大長度只有3個SMS:

lengths: { 
    ascii: [160, 306, 459], 
    unicode: [70, 134, 201] 
} 

this thread,計算公式如下:

  • 一個普通的ASCII短信將有160可用字符
  • 如果你傳遞給2個短信,發送2個短消息作爲1個消息(我認爲是UDH)需要一個特定的頭部。這個頭文件每個短信需要7個字符。所以,2個SMS具有(160 - 7)+(160 - 7)= 153 + 153 = 306可用字符
  • 相同的機制爲unicode的,但最大長度是70和UDH需要3個字符

所以你必須開發一種計算這個最大可用字符的短信給定數量的功能,我建議:

function calculateSmsMaxLength (isUnicode, writtenSmsNb) { 

       if (isUnicode) { 
        if (writtenSmsNb == 0) { 
         return 70; 
        } else { 
         return (70 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 3); 
        } 
       } else { 
        if (writtenSmsNb == 0) { 
         return 160; 
        } else { 
         return (160 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 7); 
        } 
       } 
      } 

isUnicode:真的還是假的,這取決於短信

writtenSmsNb的內容:數量的短信你想計算最大avai標籤字符。

最後,這是... mmmh代碼,可以說5個最大的手機短信:

(function($){ 
$.fn.smsArea = function(options){ 

    var e = this, 
     cutStrLength = 0, 

     s = $.extend({ 

      cut: true, 
      maxSmsNum: 5, 
      interval: 400, 

      counters: { 
       message: $('#smsCount'), 
       character: $('#smsLength') 
      }, 

      calculateSmsMaxLength : function (isUnicode, writtenSmsNb) { 

       if (isUnicode) { 
        if (writtenSmsNb == 0) { 
         return 70; 
        } else { 
         return (70 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 3); 
        } 
       } else { 
        if (writtenSmsNb == 0) { 
         return 160; 
        } else { 
         return (160 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 7); 
        } 
       } 
      } 
     }, options); 


    e.keyup(function(){ 

     clearTimeout(this.timeout); 
     this.timeout = setTimeout(function(){ 

      var smsType, 
       smsLength = 0, 
       smsCount = -1, 
       charsLeft = 0, 
       text = e.val(), 
       isUnicode = false; 

      for(var charPos = 0; charPos < text.length; charPos++){ 
       switch(text[charPos]){ 
        case "\n": case "[": case "]": case "\\": case "^": case "{": case "}": case "|": case "€": 
         smsLength += 2; 
        break; 

        default: 
         smsLength += 1; 
       } 

       if(text.charCodeAt(charPos) > 127 && text[charPos] != "€") 
        isUnicode = true; 
      } 

      for(var sCount = 0; sCount < s.maxSmsNum; sCount++){ 

       smsType = s.calculateSmsMaxLength(isUnicode, sCount); 
       cutStrLength = smsType; 
       if(smsLength <= smsType){ 

        smsCount = sCount + 1; 
        charsLeft = smsType - smsLength; 
        break 
       } 
      } 

      if(s.cut) e.val(text.substring(0, cutStrLength)); 
      smsCount == -1 && (smsCount = s.maxSmsNum, charsLeft = 0); 

      s.counters.message.html(smsCount); 
      s.counters.character.html(charsLeft); 

     }, s.interval) 
    }).keyup(); 

}}(jQuery)); 


//Start 
$(function(){ 
    $('#smsText').smsArea({maxSmsNum:5}); 
}) 

只要改變這兩個「maxSmsNum」值來設置要允許最大短信。有關信息,理論上最大值爲255,但實際上,發送時最多爲6到8 sms。