今天在做项目的时刻有一个这样的需求。就是在发送信息的时刻。要时时的提示用户的可输入字符数和已经输入的字符数。。后面自己做了一个的方法实现的。如有不足之处还请大家原谅。。现把代码共享如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<style type="text/css" >
#SendMSG_3
{
height:60px;width:510px
}
</style>
<title>即时发送信息</title>
<script language="javascript" type="text/javascript">
function gbcount(message,total,used,remain)
{
var max;
max = total.value;
if (message.value.length > max) {
message.value = message.value.substring(0,max);
used.value = max;
remain.value = 0;
alert("短信不能超过 "+max+" 个字!");
}
else {
used.value = message.value.length;
remain.value = max - used.value;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="SendMSG_3"><div style="float:left;line-height:60px; color:Blue">短信发送内容:</div><div style="float:left;">
<asp:TextBox ID="TSendContent" runat="server"
style="border:1px solid #eeeeee;width:420px;height:60px;" Rows="4"
TextMode="MultiLine" MaxLength="70" οnkeydοwn=gbcount(this.form.TSendContent,this.form.total,this.form.used,this.form.remain); οnkeyup=gbcount(this.form.TSendContent,this.form.total,this.form.used,this.form.remain);></asp:TextBox></div></div>
</br></br></br></br>
<div style="line-height:40px;height:40px">最多字数:<INPUT disabled maxLength="4" name="total" size="3" value="100">
已用字数:<INPUT disabled maxlength="4" name="used" size="3" value="0">
剩余字数:<INPUT disabled maxlength="4" name="remain" size="3" value="100"></div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
效果图: