TextBox的宽度随输入的文本的大小而改变的js代码
(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )
复制代码 代码如下:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"><!--
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
TextBox1.Attributes.Add("onkeyup", "LimitText()");
}
}
// --></script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript"><!--
function LimitText()
{
//得到此时文本框的字符数
var Len=document.getElementById('TextBox1').value.length;
//得到文本框的默认值5
var TxtSize=document.getElementById('TextBox1');
if(TxtSize.size>20)
{
TxtSize.size=20;
TxtSize.value=TxtSize.value.substring(0,20);
document.getElementById('ban').innerText="不能超过20个字符!";
}
else
{
TxtSize.size=Len+1;
}
}
// --></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" size="5"></asp:TextBox>
<span id="ban"></span>
</div>
</form>
</body>
</html>
原文来自:http://blog.csdn.net/ws_hgo
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"><!--
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
TextBox1.Attributes.Add("onkeyup", "LimitText()");
}
}
// --></script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript"><!--
function LimitText()
{
//得到此时文本框的字符数
var Len=document.getElementById('TextBox1').value.length;
//得到文本框的默认值5
var TxtSize=document.getElementById('TextBox1');
if(TxtSize.size>20)
{
TxtSize.size=20;
TxtSize.value=TxtSize.value.substring(0,20);
document.getElementById('ban').innerText="不能超过20个字符!";
}
else
{
TxtSize.size=Len+1;
}
}
// --></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" size="5"></asp:TextBox>
<span id="ban"></span>
</div>
</form>
</body>
</html>
原文来自:http://blog.csdn.net/ws_hgo
下一篇:ASP.NET FileUpload 上传图片实例