Inf*_*Dev 25 html asp.net-mvc-3
我试图限制在textarea中输入的字符数,如下所示:
<div>
@Html.TextAreaFor(x => x.StudentName, new { @maxlength = "1000" })
</div>
Run Code Online (Sandbox Code Playgroud)
这在Firefox和Chrome中运行良好,但maxlength属性在IE中不起作用.我怎么做??
Dar*_*rov 17
该maxlength属性不是<textarea>HTML 4.01中的标准.它在HTML5中定义,但我猜IE没有实现它.要使其适用于所有浏览器,您可以使用javascript.这是一个例子.
Wal*_*osz 12
这是我基于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://stackoverflow.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();
}
}
});
Run Code Online (Sandbox Code Playgroud)
IE8的链接:http://jsfiddle.net/hjPPn/show
此外,如果您想知道哪些浏览器支持maxlength:https://caniuse.com/#search=maxlength
| 归档时间: |
|
| 查看次数: |
56985 次 |
| 最近记录: |