textarea字數限制

1、input、textarea都有maxlength屬性,但是textarea不兼容ie8/9,input兼容ie8/9。


2、同時綁定onchange、onkeydown、onkeyup,ie8/9下解決不了右鍵粘貼問題。


3、上代碼……

<textarea rows="7" cols="12" style="resize: none;" id="message" maxlength="255"></textarea>

<div>還可輸入<b id="num" style="color:#ff0000;font-size: 13px;">255</b>字</div>

<script>

  $("#message").on("input propertychange", function() {  

 var $this = $(this),  

    _val = $this.val(),

    count = "";  

 var max = parseInt($this.attr("maxlength"));//最大限制字數

 if (_val.length > max) {  

    $this.val(_val.substring(0, max));  

 }  

 count = max - $this.val().length;  

 $("#num").text(count);  

  });

</script>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章