我有以下代码正常工作;
var text_max = 459;
var text_used = 0;
var sms_count = 0;
$('#leftstring').html(text_max + '');
$('#usedstring').html(text_used + '');
$('#sms_count').html(sms_count + '');
$(document).on("keyup","#Message", function() {
var text_length = $('#Message').val().length;
var text_remaining = text_max - text_length;
var text_completed = text_used + text_length;
if(text_length <= 152) {
var smscounts =+ parseInt(smscounts)+1;
}
$('#leftstring').html(text_remaining + '');
$('#usedstring').html(text_completed + '');
$('#sms_count').html(smscounts + '');
});
问题与sms_count
正如我所尝试的那样:我有459
个字符的限制,因此我想将其除以3
,因此当字符达到{时{1}},它应该计为152
,然后对于下一个1
,它应该计为152
,然后对于最后2
它应该计为152
< / p>
答案 0 :(得分:1)
您可以使用Math.ceil()
查找必须发送的最少邮件数。
var per_text = 152
$(document).on("keyup","#Message", function() {
var text_length = $('#Message').val().length;
var text_remaining = text_max - text_length;
var text_completed = text_used + text_length;
var smscounts = Math.ceil(text_length / per_text);
$('#leftstring').html(text_remaining + '');
$('#usedstring').html(text_completed + '');
$('#sms_count').html(smscounts + '');
});
答案 1 :(得分:0)
这应该从逻辑上完成你所说的:
...
if(text_length <= 152) {
var smscounts =+ parseInt(smscounts)+1;
} else if (text_length <= 304) {
//do logic
} else { // it's > 304
//do logic
}
...
答案 2 :(得分:0)
这有效:
var sms=152,text_max=3*sms; // good idea from caliburn, thanx ;-)
$(document).on("keyup","#Message", function() {
var text_length = $('#Message').val().length;
$('#leftstring').html((text_max - text_length) + '');
$('#usedstring').html((text_length % sms) + '');
$('#sms_count').html(Math.ceil(text_length / sms) + '');
});
答案 3 :(得分:0)
你想要这个
smscounts = Math.ceil(text_length / 152);
Math.ceil会向上舍入到最接近的整数。由于JavaScript划分有点不稳定,你可能不得不这样做
smscounts = Math.floor(text_length / 152) + 1;