问题描述
大家好,
我正在一个项目中,我的文本框必须根据用户输入(具体宽度)进行调整.我找到了这段代码,但是它的高度有所调整,我认为您必须为每个文本框创建多个功能,因为它使用document.getElementById.是否可以根据用户输入来调整其宽度大小?该代码是否有其他方法可以使其适用于其他文本框?提前谢谢.
1.控件声明:
Hello guys,
I''m working on a project in which my textboxes will have to resize based on the user input, the width to be specific. I found this code but it''s the height that resizes and i think that you have to make multiple functions for each textboxes since it uses document.getElementById. Is it possible to resize its width based on user input? Is there an alternative for this code to make it applicable to other textboxes? Thanks in advance.
1. Control declaration:
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this, event)" Rows="2" />
2. JavaScript函数:
2. JavaScript function:
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;
}
}
推荐答案
1. Control declaration:
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this)" Rows="2" />
2. JavaScript函数:
2. JavaScript function:
function AutoExpand(txtbox)
{
txtbox.style.height = "1px";
txtbox.style.height = (25 + txtbox.scrollHeight) + "px";
}
这篇关于根据ASP.net中的用户输入调整文本框的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!