DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> Javascript實現真實字符串剩余字數提示的實例代碼
Javascript實現真實字符串剩余字數提示的實例代碼
編輯:關於JavaScript     

復制代碼 代碼如下:
//文本框剩余字數提示(字符大小)
function textLimitCheckSj(thisArea, maxLength, SpanId) {
    var str = thisArea.value;
    if (getChrLen(str, maxLength) > maxLength * 2) {
        thisArea.value = str.substring(0, x-1);
    }
    else {
        var varss = '(剩余字數:' + Math.floor((maxLength * 2 - getChrLen(str, maxLength)) / 2) + ')';
        document.getElementById(SpanId).innerHTML = varss;
    }
}
function getChrLen(str, maxLength) {
    var realLength = 0, len = str.length, charCode = -1;
    x = 0;
    for (; (x < len) && (realLength <=maxLength * 2); x++) {
        charCode = str.charCodeAt(x);
        if (charCode >= 0 && charCode <= 128)
            realLength += 1;
        else
            realLength += 2;
    }
    return realLength;
}
?
<asp:TextBox ID="txtOwner_Name" runat="server" Width="200px" BackColor="LightYellow"
                        onkeyup="textLimitCheckSj(this,50,'spOwner_Name')"></asp:TextBox><span id="spOwner_Name"
                            style="color: #808080"><em>(50字以內)</em></span>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved