char计数器不适用于粘贴事件

时间:2017-03-02 09:32:18

标签: javascript asp.net jscript

我编写了一个代码,用于计算文本框内的字符。 代码工作得很好唯一的问题是,当我将文本放入文本框时,我必须按任意键,以便系统开始计算。

请你帮我解决这个问题

function GetAlhpa(text) {
    var gsm = "@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞ^{}\[~]|€ÆæßÉ!\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
    var i = 0;
    while (i <= String(text).length) {
        if (gsm.indexOf(String(String(text).charAt(i))) == -1 && (String(text).charCodeAt(i) != 32) && (String(text).charCodeAt(i) != 27) && (String(text).charCodeAt(i) != 10) && (String(text).charCodeAt(i) != 13)) {
            UniCodestring = " Unicode ";
            Countsms = 70;
            if ($('#SndSms_Message').val().length > 70)
                Countsms = 67;

            return;
        }

        i++;
    }
    Countsms = 160;
    UniCodestring = "";
    if ($('#SndSms_Message').val().length > 160)
        Countsms = 153;

}
var Countsms = 160;
var UniCodestring = "";
var CounterSmsLen = 0;
var Two = "|^€{}[]~";





function GetCountSms() {




document.getElementById('SndSms_Message').addEventListener('input', function (e) {
        var target = e.SndSms_Message,
            position = SndSms_Message.selectionStart;





    ConvertGreek();
    CounterSmsLen = $('#SndSms_Message').val().length;
    GetAlhpa($('#SndSms_Message').val());

    var i = 0;
    while (i < String(Two).length) {
        var oldindex = -1;
        while (String($('#SndSms_Message').val()).indexOf(String(String(Two).charAt(i)), oldindex) > -1) {
            //if ( String($('#SndSms_Message').val()).indexOf(String(String(Two).charAt(i))) > -1){
            CounterSmsLen += 1;
            oldindex = String($('#SndSms_Message').val()).indexOf(String(String(Two).charAt(i)), oldindex) + 1;


            console.log(i);
        }
        i++;

    }


SndSms_Message.selectionEnd = position;    // Set the cursor back to the initial position.
    });




    if ($('#SndSms_Message').val().length == 0)
        CounterSmsLen = 0;

    $('#SndSms_Count').html('  ' + CounterSmsLen + ' Characters' + UniCodestring + ' <br /> ' + Math.ceil(CounterSmsLen / Countsms) + '  Sms');
countsmsnumber=Math.ceil(CounterSmsLen / Countsms);
}
var greekchar = "ΑΒΕΖΗΙΚΜΝΟΡΤΥΧ";
var englishchar = "ABEZHIKMNOPTYX";
function ConvertGreek() {
    var str = $('#SndSms_Message').val();

    var i = 0;
    while (i < String(greekchar).length) {
        str = str.replace(new RegExp(String(greekchar).charAt(i), 'g'), String(englishchar).charAt(i));

        i++;
    }
    $('#SndSms_Message').val(str);

P.S。 如果我将数字粘贴到文本框中,它将计算正确,但如果我粘贴字符,它将不计算它们。

1 个答案:

答案 0 :(得分:0)

您需要键盘更改事件才能处理粘贴事件。

document.getElementById('SndSms_Message').addEventListener("keyup", function() {
   //your code here
});

example