我需要在我的应用程序中使用文本框(多行)。它需要随着用户输入文本而增长。我希望该文本框在没有任何滚动条的情况下增长。我尝试了 allow=resize:null
但它只是阻止文本框被拉伸(stretch)。我想要的只是一个文本框,正如我们在 facebook 评论/共享区域中看到的那样,它的增长简单,侧面没有任何滚动条。我希望我已经清楚地解释了它。
最佳答案
您可以使用如下内容:(来源: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 .
A plugin for jQuery which changes the dimensions of input elements to suit the amount of data entered. It operates on textarea, input[type=text] and input[type=password] elements.
使用方法如下:
$('textarea#foo').autoResize();
您可以传递选项:
$('textarea#foo').autoResize({
maxHeight: 200,
minHeight: 100
});
关于facebook - 如何创建一个没有滚动条动态增长的多行文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9943329/