在文本框中按下退格键时触发的事件 [英] event fired when backspace is pressed in a textbox
本文介绍了在文本框中按下退格键时触发的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有多行文本框.当用户在文本框中键入内容时,我使用javascript函数显示字符计数.
代码如下:
文本框和标签-
Hi all,
i have multiline textbox. i use javascript funtion to display the characted count when the user types in the text box.
The code is like so:
text box and label-
<asp:TextBox ID="txtSignature" runat="server" TextMode = "MultiLine"
MaxLength="160" Enabled = "false" ></asp:TextBox>
<asp:Label ID="lblCharacters" runat="server"></asp:Label>
javascript函数来更新标签-
javascript function to update the label -
function DisplayCount()
{
var length = document.getElementById('<%= txtSignature.ClientID %>').value.length;
document.getElementById('<%= lblCharacters.ClientID%>').innerText = length+ " of 160 used!";
}
在页面加载时注册事件-
txtSignature.Attributes.Add("onkeypress","javascript:return DisplayCount();");
如何捕获退格键并删除按键并更新字符数?
任何帮助将不胜感激.
谢谢.
registering the event on page load -
txtSignature.Attributes.Add("onkeypress", "javascript:return DisplayCount();");
How can i catch the backspace and delete key presses and update the character count?
ANy help will be appreciated.
Thanks.
推荐答案
处理onkeydown事件.我看不到任何地方您的代码会引发任何事件.在那种情况下调用您的方法.
Handle the onkeydown event. I don''t see anywhere that your code hooks up any event at all. Call your method in that event.
Christian是正确的.我认为 JQuery链接提供了您想要的东西.以下是我尝试过的代码(有效):
Christian is right. I think this JQuery link provides what you want. Below is the code I tried (It worked):
<script type="text/javascript" src="jquery-1.6.js"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" language="javascript" >
(document).ready(function(){
(document).ready(function() {
这篇关于在文本框中按下退格键时触发的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文