Home JQuery控制输入字段的长度
Post
Cancel

JQuery控制输入字段的长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$("#TEST").attr("onkeyup", "checkLength(this, 50);"); // 设置触发事件

var checkLength = function(dom, maxLength) {
    var l = 0;
    for(var i = 0; i < dom.value.length; i++) {
        if (/[\u4e00-\u9fa5]/.test(dom.value[i])) {
            l+=2;
        } else {
            l++;
        }
        if (l > maxLength) {
            dom.value = dom.value.substr(0,i);
            break;
        }
    }
};  
This post is licensed under CC BY 4.0 by the author.