我需要在我的应用程序中使用文本框(多行)。它需要随着用户输入文本而增长。我希望该文本框在没有任何滚动条的情况下增长。我尝试了allow=resize:null
,但它只是阻止了文本框被拉伸。我想要的是一个文本框,正如我们在Facebook评论/共享区域中看到的那样,只是侧面没有任何滚动条而增长。我希望我能整齐地解释它。
答案 0 :(得分:2)
您可以使用以下内容:(来源:Expandable or Auto-Resize TextBox Height by Colt Kwong)
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine"
style="overflow:hidden" onkeyup="AutoExpand(this, event)" Rows="2" />
使用此javascript:
function AutoExpand(txtBox, event)
{
if (event.keyCode == "13" || event.keyCode == "8") {
var therows = 0
var thetext = document.getElementById(txtBox.id).value;
var newtext = thetext.split("\n");
therows += newtext.length
document.getElementById(txtBox.id).rows = therows;
return false;
}
}
或者如果你喜欢jquery,你应该看一下jQuery autoResize Plugin。
jQuery的插件,它将输入元素的尺寸更改为 适合输入的数据量。它在textarea上运作, 输入[type = text]并输入[type = password]元素。
用法如下:
$('textarea#foo').autoResize();
您可以传递选项:
$('textarea#foo').autoResize({
maxHeight: 200,
minHeight: 100
});