如何使readonly <s:textarea>适合其内容</s:textarea>

时间:2014-09-26 15:16:51

标签: javascript html struts2 textarea struts-tags

我的格式有一个textarea:

<s:textarea key = "appInfo.collection" 
       required = "true" 
       readonly = "true"
       cssClass = "form-control" 
           rows = "8"/>

还有更少的textarea字段。我想让这些textareas适合他们的内容在readonly模式。

由于我已指定rows=8,如果内容仅填充2行,则有时会显示空白区域;如果内容超过8行,则会显示滚动条。

如何根据内容为这些textareas提供正确的尺寸,没有滚动条?

1 个答案:

答案 0 :(得分:1)

您可以在我的评论中使用the answer链接。

关于你的评论

  

我的文本区域处于读取模式,我没有可用的键盘功能

您不需要任何keyup处理程序:因为它是只读的,所以在页面加载后执行一次:

<s:textarea id = "myTextarea"
           key = "appInfo.collection" 
      required = "true" 
      readonly = "true"
      cssClass = "form-control" 
          rows = "8"/>
function autoheight(a) {
    if (!$(a).prop('scrollTop')) {
        do {
            var b = $(a).prop('scrollHeight');
            var h = $(a).height();
            $(a).height(h - 5);
        }
        while (b && (b != $(a).prop('scrollHeight')));
    };
    $(a).height($(a).prop('scrollHeight') + 20);
}

$(function(){ 
    // call the function in document.ready 
    autoheight($("#myTextarea"));
});