使用Javascript的字符计数在移动设备上不起作用

使用Javascript的字符计数在移动设备上不起作用,javascript,jquery,mobile,Javascript,Jquery,Mobile,这是我用来计算textarea中的字符数的。这在桌面设备中运行良好。但在移动设备中,存在一些计数错误。例如,在某些设备中,它最多占用301个字符,此后退格就不起作用 function checkcharcount(e, eleid) { var max = 300; var val = document.getElementById(eleid).value; document.getElementById("cccount").innerTe

这是我用来计算textarea中的字符数的。这在桌面设备中运行良好。但在移动设备中,存在一些计数错误。例如,在某些设备中,它最多占用301个字符,此后退格就不起作用

function checkcharcount(e, eleid)
  {
        var max = 300;
        var val = document.getElementById(eleid).value;
        document.getElementById("cccount").innerText = val.length;

        if (e.which < 0x20) {
            // e.which < 0x20, then it's not a printable character
            // e.which === 0 - Not a character
            return;     // Do nothing
        }
        if (document.getElementById(eleid).value.length == max) {
            e.preventDefault();
        } else if (document.getElementById(eleid).value.length > max) {
            // Maximum exceeded
            document.getElementById(eleid).value = document.getElementById(eleid).value.substring(0, max);
        }
        document.getElementById("cccount").innerText = val.length;
  }
函数checkcharcount(e,eleid)
{
var max=300;
var val=document.getElementById(eleid).value;
document.getElementById(“cccount”).innerText=val.length;
如果(如<0x20){
//如果小于0x20,则它不是可打印字符
//e.which===0-不是字符
return;//什么也不做
}
if(document.getElementById(eleid.value.length==max){
e、 预防默认值();
}else if(document.getElementById(eleid.value.length>max){
//超过最大值
document.getElementById(eleid).value=document.getElementById(eleid).value.substring(0,最大值);
}
document.getElementById(“cccount”).innerText=val.length;
}
非常感谢您的建议。
提前谢谢。

为什么不使用
maxlength
属性?是的,我可以使用它。但是我正在寻找基于javascript/Jquery的解决方案。为什么不使用
maxlength
属性呢?是的,我可以使用它。但我正在寻找基于javascript/Jquery的解决方案。