jQuery:文本框键盘触发两次 [英] jQuery: textbox keyup firing twice
问题描述
我有一个文本框并分配了以下功能(这是分配的唯一功能):
I'm having a textbox and assigned the following function (it's the only function assigned):
txt.bind("keyup",function(event){
if(event.keyCode==13)
{
var nu = $("#debug").html();
nu+="<br>enter";
$("#debug").html(nu);
}
});
奇怪的是,它实际上触发了两次,因此在调试窗口中两次显示输入". 有谁知道是什么原因造成的?
The strange thing is that it's actually firing twice, thus displaying "enter" twice in my debug window. Does anyone know what is causing this?
推荐答案
我知道已经有一段时间了,但是我很惊讶没有人建议:
I know it's been quite awhile, but I'm surprised nobody suggested:
txt.unbind();
txt.bind("keyup",function(event){
if(event.keyCode==13)
{
var nu = $("#debug").html();
nu+="<br>enter";
$("#debug").html(nu);
}
});
如果txt已经被绑定,请在新绑定之前调用 unbind 来解决该问题.当然,最好弄清楚为什么要绑定两次.这只是发生在我身上,因为我不小心将JavaScirpt文件包含了两次.呼叫取消绑定可以帮助我确定问题所在.
If somehow txt got bound already, calling unbind before your new bind should fix it. Of course it's preferable to figure out why it's being bound twice. This just happened to me, because I accidentally had my JavaScirpt file included twice. Calling unbind helped me determine that was the problem.
这篇关于jQuery:文本框键盘触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!