2012-01-16 32 views
0

我在我的MVC2.0應用程序中使用HTML.TEXTAREA,但我不能限制textarea允許用戶只輸入255個字符。 我還提供了<%html.textarea(「Name」,new {@maxlength =「255」})%>但仍然無法達到目標。 請給我提供任何解決方案。如何在Html.textarea中添加Maxlength?

感謝

回答

0

您可以編寫Java腳本,並限制文本區域尺寸。

<script type="text/javascript"> 

     $(function() { 
      $("#id").keypress(function() { 
      var maxlen = 100; 
      if ($(this).val().length > maxlen) { 
       return false; 
      } 
     }) 
     }); 
</script> 
0

一個非常簡單的方法是使用SubString。它不是你需要的,你可以自己寫。

例如:

var textAreaObject = document.getElementById("TextAreaId"); 
    if (textAreaObject != null) { 
     var maxlength = 60; //Give the desired value for your Maxlength 
     if (textAreaObject .value.length > maxlength) { 
      //Substring from the entered text. Easy-peezy 
      textAreaObject .value = textAreaObject .value.substring(0, maxlength); 
     } 
    } 

工作無處不在,隨時隨地。並將其用於粘貼和按鍵事件。快樂的編碼。