2013-03-28 68 views
0

我已经使用了下面的jQuery代码来显示有多少字符留在textarea中,它在所有浏览器上的工作都很好。
但是当我使用退格键和删除一些字符,然后它显示有多少字符留在Mozilla上罚款,但它不适用于IE浏览器,Chrome浏览器,Safari浏览器。jquery限制输入字符不适用于IE/Chrome/safari

这是演示链接:http://jsfiddle.net/ckWNM/

jQuery(document).ready(function($) { 
    var max = 10; 
    $('textarea.max').keypress(function(e) { 
     $("#counter").html("characters left : " +(10 - this.value.length)); 
     if (e.which < 0x20) { 
      return; 
     } 
     if (this.value.length == max) { 
      e.preventDefault(); 
     } else if (this.value.length > max) { 
      // Maximum exceeded 
      this.value = this.value.substring(0, max); 
     } 
    }); 
}); //end if ready(fn) 

回答

0

更新上keyup留下的文字,以及可以在Chrome中解决这个问题。

jsFiddle

jQuery(document).ready(function($) { 
    var max = 10; 
    $('textarea.max') 
     .keypress(refreshAmount) 
     .keyup(refreshAmount); 
}); //end if ready(fn) 

function refreshAmount(e) { 
    $("#counter").html("characters left : " +(10 - this.value.length)); 
    if (e.which < 0x20) { 
     return; 
    } 
    if (this.value.length == max) { 
     e.preventDefault(); 
    } else if (this.value.length > max) { 
     // Maximum exceeded 
     this.value = this.value.substring(0, max); 
    } 
}