How to set the maxLength of a TextArea so that it works across all browsers?

北城余情 提交于 2019-12-13 21:04:06

问题


I wrote the below maxLength restriction on the textarea control and it works fine in IE 9.0 but doesn't work with IE 8.0.

<textarea name="commentString" rows="7" maxlength="2000">some data </textarea>

How can I set the maxLength so that it works across all browsers? would JQuery have any helper method for this?


回答1:


   $('textarea').keypress(function() {
  return $(this).val().length < 50;
});



回答2:


It will not working below IE9.

Use this code it will work for below IE 9 version. only change version in if condtion.

if(navigator.appVersion.indexOf("MSIE .9")!=-1)
                                {
                                    $('#inputid').bind('keyup blur', function () {
                                        var $this = $(this);
                                        var len = $this.val().length;
                                        var maxlength = 3;
                                        if (maxlength && len > maxlength) {
                                            var inputvalue= $this.val().slice(0, maxlength);
                                            $this.val("");
                                            $this.val(inputvalue);
                                        }
                                    });
                                }


来源:https://stackoverflow.com/questions/18845866/how-to-set-the-maxlength-of-a-textarea-so-that-it-works-across-all-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!