这篇文章主要介绍jQuery如何限制textarea的字符数量,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
限制textarea的字符数量
jQuery.fn.maxLength = function(max){this.each(function(){var type = this.tagName.toLowerCase();var inputType = this.type? this.type.toLowerCase() : null;if(type == "input" && inputType == "text" || inputType == "password"){//Apply the standard maxLengththis.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);}};}});};//Usage:$('#gbin1textarea').maxLength(500);
以上是“jQuery如何限制textarea的字符数量”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!