如何使用jquery创建n个sms计数?
像这个代码只有3个短信计数。我需要使用类似相同代码的N个sms计数。 怎么办?
$('#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短信。如果任何一个知道意味着发送查询。
答案 0 :(得分:0)
在此代码中,作者给出的数组的最大长度仅为3 sms:
lengths: {
ascii: [160, 306, 459],
unicode: [70, 134, 201]
}
根据this thread,它计算如下:
所以你必须开发一个函数来计算给定数量的短信的最大可用字符数,我建议:
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:true或false,具体取决于短信的内容
writtenSmsNb:要为其计算最大可用字符数的短信数。
最后,这是... 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});
})
只需更改&#34; maxSmsNum&#34;值以设置您要允许的最大sms。有关信息,理论上最大值为255,但实际上,通过发送它通常最多为6到8个sms。