2013-03-28 3 views
0

다음 jquery 코드를 사용하여 모든 브라우저에서 textarea의 입력에 얼마나 많은 문자가 남았는지 보여줍니다.
하지만 백 스페이스를 사용하여 일부 문자를 삭제하면 Mozilla에서는 얼마나 많은 문자가 남았지 만 IE, Chrome, Safari에서는 작동하지 않습니다. http://jsfiddle.net/ckWNM/IE/Chrome/Safari에서 Jquery 한도가 작동하지 않습니다.

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에 남아 업데이트 :

데모 링크입니다.

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); 
    } 
} 
관련 문제