问题
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