I am trying to limit the number of characters entered in a textarea as shown:
我试图限制在一个textarea中输入的字符数,如下所示:
<div>
@Html.TextAreaFor(x => x.StudentName, new { @maxlength = "1000" })
</div>
This works fine in Firefox and Chrome but the maxlength attribute doesn't work in IE. How do I do that??
这在Firefox和Chrome中都很好用,但是maxlength属性在IE中不起作用。我该怎么做呢?
4 个解决方案
#1
17
The maxlength
attribute is not standard for <textarea>
in HTML 4.01. It is defined in HTML5 though but I guess IE doesn't implement it. To make it work across all browsers you could use javascript. Here's an example.
在HTML 4.01中,maxlength属性不是
#2
12
Here is my jQuery based solution. Works well in IE9, a little wonky in IE8.
这是我的基于jQuery的解决方案。在IE9中工作得很好,在IE8中有点不稳定。
// textarea event handler to add support for maxlength attribute
$(document).on('input keyup', 'textarea[maxlength]', function(e) {
// maxlength attribute does not in IE prior to IE10
// http://*.com/q/4717168/740639
var $this = $(this);
var maxlength = $this.attr('maxlength');
if (!!maxlength) {
var text = $this.val();
if (text.length > maxlength) {
// truncate excess text (in the case of a paste)
$this.val(text.substring(0,maxlength));
e.preventDefault();
}
}
});
http://jsfiddle.net/hjPPn/
link for IE8: http://jsfiddle.net/hjPPn/show
IE8的链接:http://jsfiddle.net/hjPPn/show
Native Browser Support
Also, in case you're wondering which browsers do support maxlength
: https://caniuse.com/#search=maxlength
另外,如果您想知道哪些浏览器支持maxlength: https://caniuse.com/#search=maxlength。
#3
3
You can use javascript as well, which is way easier:
您也可以使用javascript,这很容易:
<textarea name="question" cols="100" rows="8" maxlength="500px" onKeyPress="return ( this.value.length < 1000 );"></textarea>
#4
1
Use the following Jquery - for eg, if you want to restrict the length to 48
使用下面的Jquery—例如,如果您想将长度限制为48。
$("textarea").keypress(function(e){
var lengthF = $(this).val();
if (lengthF.length > 48){
e.preventDefault();
}
});
#1
17
The maxlength
attribute is not standard for <textarea>
in HTML 4.01. It is defined in HTML5 though but I guess IE doesn't implement it. To make it work across all browsers you could use javascript. Here's an example.
在HTML 4.01中,maxlength属性不是
#2
12
Here is my jQuery based solution. Works well in IE9, a little wonky in IE8.
这是我的基于jQuery的解决方案。在IE9中工作得很好,在IE8中有点不稳定。
// textarea event handler to add support for maxlength attribute
$(document).on('input keyup', 'textarea[maxlength]', function(e) {
// maxlength attribute does not in IE prior to IE10
// http://*.com/q/4717168/740639
var $this = $(this);
var maxlength = $this.attr('maxlength');
if (!!maxlength) {
var text = $this.val();
if (text.length > maxlength) {
// truncate excess text (in the case of a paste)
$this.val(text.substring(0,maxlength));
e.preventDefault();
}
}
});
http://jsfiddle.net/hjPPn/
link for IE8: http://jsfiddle.net/hjPPn/show
IE8的链接:http://jsfiddle.net/hjPPn/show
Native Browser Support
Also, in case you're wondering which browsers do support maxlength
: https://caniuse.com/#search=maxlength
另外,如果您想知道哪些浏览器支持maxlength: https://caniuse.com/#search=maxlength。
#3
3
You can use javascript as well, which is way easier:
您也可以使用javascript,这很容易:
<textarea name="question" cols="100" rows="8" maxlength="500px" onKeyPress="return ( this.value.length < 1000 );"></textarea>
#4
1
Use the following Jquery - for eg, if you want to restrict the length to 48
使用下面的Jquery—例如,如果您想将长度限制为48。
$("textarea").keypress(function(e){
var lengthF = $(this).val();
if (lengthF.length > 48){
e.preventDefault();
}
});