在标签上显示文本框数量 [英] Displaying text box count on a label
问题描述
大家好,
我有一个ASP文本框和一个标签,用于按如下方式显示字符数:
Hi all,
I have a asp text box and a label to display the count of characters as so:
<asp:TextBox ID="txtSignature" runat="server" TextMode = "MultiLine"
MaxLength="160" ></asp:TextBox> <asp:Label ID="lblCharacters" runat="server"></asp:Label>
我已经放入Jscript函数来显示计数:
i have put in Jscript functions to display the count:
<script language ="javascript" type="text/javascript">
function DisplayCount() {
var length = document.getElementByID('<%= txtSignature.ClientID %>').value.length;
document.getElementByID('<%= lblCharacters %>').value = length;
alert(length);
}
</script>
页面加载事件具有以下代码:
The page load event has the following code:
txtSignature .Attributes.Add("javascript:onkeydown();", "javascript:return DisplayCount();");
txtSignature.Attributes.Add("javascript:onkeypress();", "javascript:return DisplayCount();");
当我在文本框中键入内容时,我仍然看不到标签上的字数.有人知道概率是多少吗?
谢谢.
I still cant see teh count on teh label when i type in the textbox. Any one knows what the prob might be?
Thanks.
推荐答案
只需将您的JavaScript代码替换为此.
Just replace your javascript code with this.
<script language="javascript" type="text/javascript">
function DisplayCount() {
var length = document.getElementById(''<%=txtSignature.ClientID%>'').value.length;
document.getElementById(''<%=lblCharacters.ClientID%>'').innerText = length;
alert(length);
}
</script>
1)将 D 替换为document.getElementByID
中的 d .应该是document.getElementById
2)对于标签,您必须使用innerText
设置值.
并且在页面加载中还可以像这样更改代码.
1) Replace D with d in document.getElementByID
. It should be document.getElementById
2) For label you have to use innerText
to set the value.
And in page load also change the code like this.
txtSignature.Attributes.Add("onkeypress", "javascript:return DisplayCount();");
仅keypress
或keydown
就足够了.
Only keypress
or keydown
is enough.
document.getElementByID('<%= lblCharacters %>').innerHTML = length;
document.getElementByID('<%= lblCharacters %>').innertext = length;
我可以看到至少一个错误:必须为document.getElementById
(大写).代码区分大小写,您知道...
—SA
I can see at least one bug: must bedocument.getElementById
(capitalization). Code is case-sensitive, you know…
—SA
这篇关于在标签上显示文本框数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!