美文网首页
完美解决textarea字数限制[转载]

完美解决textarea字数限制[转载]

作者: willeny | 来源:发表于2018-07-27 21:13 被阅读0次

原文地址:https://blog.csdn.net/fb_01/article/details/51026774

  • input、textarea都有maxlength属性,但是textarea不兼容ie8/9,input兼容ie8/9。
  • 同时绑定onchange、onkeydown、onkeyup,ie8/9下解决不了右键粘贴问题。
  • 具体代码如下:
    <textarea name="detail" id="detail" cols="" rows="" placeholder="请输入内容" style="height: 250px;"></textarea>
    <p><span id="text-count">255</span>/255</p>
    <script type="text/javascript">
            $('#detail').on("input propertychange",function(){
                var this = $(this);
                _val = $this.val(),
                count = "";
                if(_val.length > 255){
                    $this->val(_val.substring(0,255));
                }
                count = 255 - $this.val().length;
                $("#text-count").text(count);
            });
     </script>
html代码截图
js代码截图 效果图

相关文章

网友评论

      本文标题:完美解决textarea字数限制[转载]

      本文链接:https://www.haomeiwen.com/subject/ypxnmftx.html