Javascript实现真实字符串剩余字数提示的实例代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Javascript实现真实字符串剩余字数提示的实例代码

  2020-05-13 我要评论

复制代码 代码如下:

//文本框剩余字数提示(字符大小)
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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们