TextArea限制輸入長度

 

cs文件中添加文本框屬性this.TextBox.Attributes.Add("MaxLength", "200");this

 1  $(document).ready(function() {
 2             $("textarea").each(function() {
 3                 var maxLength = $(this).attr("MaxLength");
 4                 if (maxLength) {
 5                     $(this).bind("keyup", "", function(e) {
 6                         var le = $(this).val().len();
 7                         if (le > maxLength) {
 8                                 $(this).val($(this).val().substring(0,maxLength))
 9                         }
10                     });
11                 }
12             });
13         });
14         //返回字符串的實際長度, 一個漢字算2個長度
15         String.prototype.len = function() {
16             return this.replace(/[^\x00-\xff]/g, "**").length;
17         }
相關文章
相關標籤/搜索