第一步:以下是核心代码,在页面引用或进行封装都可。
<script language="javascript" type="text/javascript">
function textLimitCheck(thisArea, maxLength, SpanId)
{
if (thisArea.value.length > maxLength)
{
thisArea.value = thisArea.value.substring(0, maxLength);
thisArea.focus();
}
/*回写span的值,当前填写文字*/
var varss='(剩余字数:'+(maxLength-thisArea.value.length)+')';
document.getElementById(SpanId).innerHTML =varss;
}
</script>
第二步:文本框应用此函数
<asp:TextBox ID="txtOwner_Name" runat="server" Width="250px" MaxLength="50" onkeyup="textLimitCheck(this,50,'spOwner_Name')" BackColor="Info"></asp:TextBox>
<span id="spOwner_Name" style="color: gray"><em>(50字以内)</em></span>