使用jQuery计算字符/短信 [英] Count characters/sms using jQuery

查看:126
本文介绍了使用jQuery计算字符/短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NobleCount 和以下代码计算字符:

I count characters using NobleCount and the following code:

$('#message').NobleCount('#messageInfo',{
            max_chars: getMaxChars(),
            on_negative: function(t_obj, char_area, c_settings, char_rem){

            }
});

我想要一个像计算短信的功能,如果达到限制,接下来的160个字符是第二个短信等。我可以使用像on_update,on_positive,on_negative和block_negative这样的参数。

I want a feature like counting SMS, if the limit is reached the next 160 chars are for the second sms and so on. I can use parameters like on_update, on_positive, on_negative and block_negative.

我尝试使用modulo但是它不起作用。有什么想法?

I tried something using modulo but it does not work. Any ideas?

推荐答案

首先,字符计数非常简单。您只需要在字符串上使用 length 属性。要计算所需的SMS消息数量,您需要除以160并向上舍入(因为161个字符需要2条消息)。你的代码可能看起来像这样:

Firstly, character counting is very easy. You just need to use the length property on a string. To count the number of SMS messages needed, you'll need to divide by 160 and round up (because 161 characters requires 2 messages). Your code should probably look something like this:

HTML:

<textarea name="message" value="" id="message"></textarea>
<p>
    <span id="remaining">160 characters remaining</span>
    <span id="messages">1 message(s)</span>
</p>

jQuery:

$(document).ready(function(){
    var $remaining = $('#remaining'),
        $messages = $remaining.next();

    $('#message').keyup(function(){
        var chars = this.value.length,
            messages = Math.ceil(chars / 160),
            remaining = messages * 160 - (chars % (messages * 160) || messages * 160);

        $remaining.text(remaining + ' characters remaining');
        $messages.text(messages + ' message(s)');
    });
});

参见jsFiddle示例。

这篇关于使用jQuery计算字符/短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆