限制文本框输入字数

来源:互联网 发布:node mysql 库 编辑:程序博客网 时间:2024/06/11 22:03
jQuery.fn.maxLength=function(max){        return this.each(function(){            var type = this.tagName.toLowerCase();            var inputType = this.type? this.type.toLowerCase() : null;            if(type == "input" && inputType == "text" || inputType == "password"){                this.maxLength = max;            }else if(type=="textarea"){                this.onkeypress=function(e){                    var ob=e||event;                    var keyCode=ob.keyCode; //键盘输入编码                    var hasSelection=document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;   //获取当前页面选中的内容                    return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);                };                this.onkeyup=function(){                    if(this.value.length>max){                        this.value = this.value.substring(0,max);                    }                };            }        });    };
0 0
原创粉丝点击