javascript textarea 的最大长度不适用于 IE8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16909063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Max length of textarea is not working on IE8
提问by user1871516
From research on internet, max length attribute is not working on IE 8 and 9
根据互联网研究,最大长度属性不适用于 IE 8 和 9
To resolve the problem I tried a solution from here , it use with the other function which is for presentation textarea:
为了解决这个问题,我从这里尝试了一个解决方案,它与另一个用于演示文本区域的功能一起使用:
//Dynamic append the textarea row
function do_resize(textArea) {
while (
textArea.rows > 1 &&
textArea.scrollHeight < textArea.offsetHeight
)
{
textArea.rows--;
}
while (textArea.scrollHeight > textArea.offsetHeight)
{
textArea.rows++;
}
textArea.rows++
}
<textarea name="q<%=countNo%>_ans" rows="3" cols="55" maxlength="2000" style="resize: none;" onkeyup="new do_resize(this);" onKeyPress="return ( this.value.length < 2000);"></textarea>
The problem is , The textarea is not able to input any character after it exceed the 2000 in IE8 9 , but I can still use the copy and paste function which will exceed the textarea limit. How to fix this? thanks
问题是,在IE8 9中textarea超过2000后无法输入任何字符,但我仍然可以使用超过textarea限制的复制和粘贴功能。如何解决这个问题?谢谢
回答by Jukka K. Korpela
The code in the question effectively disables typing from keyboard when the limit has been reached. To impose the restriction on pasted content too, you need to handle other events, too. The following code truncates the textarea content to the given length. This is not good usability (you should probably signal an attempt to exceed the limit, instead of silent truncation, and you could have a character counter on the page to help the user), but it does what was asked for:
当达到限制时,问题中的代码有效地禁用了从键盘输入。要对粘贴的内容也施加限制,您还需要处理其他事件。以下代码将 textarea 内容截断为给定长度。这不是很好的可用性(您可能应该发出超出限制的尝试,而不是静默截断,并且您可以在页面上使用字符计数器来帮助用户),但它可以满足要求:
<textarea maxlength=2000
onchange="testLength(this)"
onkeyup="testLength(this)"
onpaste="testLength(this)"
></textarea>
<script>
var maxLength = 2000;
function testLength(ta) {
if(ta.value.length > maxLength) {
ta.value = ta.value.substring(0, maxLength);
}
}
</script>
回答by nt827
Just for some hstory on this, maxlength on a textarea is a new HTML5 feature which was only first supported by IE in 10. IE 9 and below never supported it.
仅就这方面的一些故事而言,textarea 上的 maxlength 是一项新的 HTML5 功能,仅在 10 中才由 IE 首次支持。IE 9 及以下从未支持它。
回答by Ravi Chothe
Use this code it will work for below IE 9 version. only change version in if condtion.
使用此代码它将适用于 IE 9 版本以下。仅在 if 条件下更改版本。
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);
}
});
}
回答by user1871516
Simply add onpaste
fixed the problem
只需添加即可onpaste
解决问题
<textarea name="q<%=countNo%>_ans" rows="3" cols="55" maxlength="2000" style="resize: none;" onkeyup="new do_resize(this);" onKeyPress="return ( this.value.length < 2000); onpaste="return ( this.value.length < 2000);"></textarea>