我们知道文本框可以有一个为maxlength的属性,可以限制文本框的长度,当时备注框textarea却没有,那么
要怎样限制备注框的长度呢?其实很简单,只有加上想这样一句话onKeyDown='if (this.value.length>=20){event.returnValue=false}'
就可以了,整个写法如下:
<textarea name="A" cols="45" rows="2" onKeyDown='if (this.value.length>=20){event.returnValue=false}'>aaaa</textarea>
    我们也可以将判断写在函数中,如果输入的长度超过显示,就显示提示信息,如下:
<html>
 <body>
  <form name = "testform">
   <textarea name="A" cols="45" rows="2" >aaaa</textarea>
   <input type="button" onclick = "checkValid()" value= "提交">
  </form>
 </body>
</html>
<script language="javascript">
function checkValid()
{
 var a = document.testform.A;
 
 if(a.value.length > 20)
 {
  alert("输入的备注框长度不能超过20个字符!");
  return false;
 }
 return true;
}
</script>

相关文章:

  • 2021-08-28
  • 2021-05-23
  • 2022-12-23
  • 2021-06-16
  • 2021-09-27
  • 2021-12-01
  • 2021-05-20
猜你喜欢
  • 2021-06-02
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
相关资源
相似解决方案