当前位置:Gxlcms > asp.net > TextBox的宽度随输入的文本的大小而改变的js代码

TextBox的宽度随输入的文本的大小而改变的js代码

时间:2021-07-01 10:21:17 帮助过:10人阅读

代码如下:
<%@ 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

人气教程排行