2013-03-28 69 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); 
    } 
}