textarea的最大长度在IE8上不起作用 [英] Max length of textarea is not working on IE8

查看:208
本文介绍了textarea的最大长度在IE8上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从互联网上的研究,最大长度属性不工作在IE 8和9上

为了解决这个问题,我尝试了一个解决方案,它与其他函数用于呈现textarea:

  //动态添加textarea行
函数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%> _ansrows =3 cols =55maxlength =2000style =resize:none; onkeyup =new do_resize(this); onKeyPress =return(this.value.length< 2000);>< / textarea>

问题在于,textarea在IE8 9中超过2000后无法输入任何字符,但我仍然可以使用超过textarea限制的复制和粘贴功能。如何解决这个问题?谢谢

解决方案

在达到限制时,问题中的代码有效地禁用键盘输入。为了对粘贴的内容施加限制,您也需要处理其他事件。以下代码将textarea内容截断为给定的长度。这不是很好的可用性(你可能应该试图超出限制而不是无声截断,并且你可以在页面上有一个字符计数器来帮助用户),但它可以满足要求:

 < textarea maxlength = 2000 
onchange =testLength(this)
onkeyup =testLength(this)
onpaste =testLength(this)
>< / textarea>
< script>
var maxLength = 2000;
函数testLength(ta){
if(ta.value.length> maxLength){
ta.value = ta.value.substring(0,maxLength);
}
}
< / script>


From research on internet, max length attribute is not working on IE 8 and 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

解决方案

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

这篇关于textarea的最大长度在IE8上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆