[英]one enter keypress runs JavaScript keypress event handler code twice
Why does one enter keypress
run through this code twice?为什么一次输入keypress
运行两次此代码? I've tried keypress
, keydown
and also event.preventdefault()
to no avail.我试过keypress
, keydown
和event.preventdefault()
都无济于事。
$(document).keydown(function (e) {
if (e.which == 13) {
alert("0")
var focado = document.getElementById(document.activeElement.id), fim = focado.id.substring(1),
inicio = focado.id.substring(0, 1),
fimseg = fim,
anterior = inicio + (fim - 1),
alert("1")
alert("focado " + focado.id + " fim " + fim + " inicio " + inicio + " fimseg " + fimseg + " anterior " + anterior);
}
});
Only after giving these 3 messages twice would it continue to the remaining code (not shown).只有在两次给出这 3 条消息后,它才会继续执行剩余的代码(未显示)。
UPDATE:更新:
This is the full program:这是完整的程序:
$(document).keydown(function (e) {
if (e.which == 13) {
var focado = document.getElementById(document.activeElement.id), fim = focado.id.substring(1),
inicio = focado.id.substring(0, 1),
fimseg = fim,
anterior = inicio + (fim - 1),
seguinte = focado;
switch (fim) {
case "1":
fimseg = "2"
break;
case "2":
fimseg = "3"
break;
case "3":
fimseg = "4"
break;
case "4":
fimseg = "5"
break;
}
seguinte = inicio + (fimseg);
if (seguinte == "p5") {
seguinte = "a1";
}
alert("focado " + focado.id + " fim " + fim + " inicio " + inicio + " fimseg " + fimseg + " anterior " + anterior + " seguinte " + seguinte);
if (focado.value.length == 0) {//enter no vazio
alert("aqui")
if (fim != 1) {//se não é p1 nem a1
alert("1")
if (document.getElementById(anterior).value.length == 0) {//se anterior vazio
alert("2")
document.getElementById(anterior).focus()
} else {//se anterior não vazio
alert("3")
if (seguinte == "a5") {//se é a4
alert("4")
document.getElementById("btnSubmit").click();
} else {//se nºao é a4
alert("5")
document.getElementById(seguinte).focus()
}
}
} else {//é p1 ou a1
document.getElementById(focado.id).focus() //foca no próprio
}
} else {
if (focado.value.length < 7 || (isNaN(focado.value))) {
document.getElementById(focado.id).innerHtml = "";
if (anterior.value.length == 0) {
document.getElementById(anterior).focus()
} else {
document.getElementById(focado.id).focus()
}
} else {
if (fim != 1) { //não é p1 nem a1
if (document.getElementById(anterior).value.length ==0) {
document.getElementById(anterior).focus()
} else {
if (seguinte == "a5") {
document.getElementById("btnSubmit").click();
} else {
document.getElementById(seguinte).focus()
}
}
} else {
document.getElementById(seguinte).focus()
}
}
}
} //não é enter
}); //fim função
When it gets to当它到达
if (focado.value.length == 0)
it goes back to the first variable declaration.它回到第一个变量声明。 This is a problem because that way the variables fimseg
and seguinte
begin that cycle with the wrong values.这是一个问题,因为这样变量fimseg
和seguinte
以错误的值开始该循环。
You seem to have a syntax error.您似乎有语法错误。 Even when I include the });
即使我包含了});
that was not properly code-wrapped in your question, I still get this error in my Firebug console:在您的问题中没有正确包装代码,我仍然在我的 Firebug 控制台中收到此错误:
SyntaxError: missing ;
before statement alert("1") <----
Try creating a sample at http://jsfiddle.net/ .尝试在http://jsfiddle.net/创建一个示例。 Then we can help you more.那么我们可以为您提供更多帮助。
EDIT:编辑:
OK, I looked at your jsfiddle.好的,我看了你的 jsfiddle。 Based on your comment, I think the main problems are:根据您的评论,我认为主要问题是:
Here is the updated, working code: https://jsfiddle.net/op96m1Ln/2/ .这是更新后的工作代码: https : //jsfiddle.net/op96m1Ln/2/ 。 I use parseInt to capture the current box and some simple logic to jump to the next box in order.我使用 parseInt 来捕获当前框和一些简单的逻辑来按顺序跳转到下一个框。 I also use regular expressions to validate the input.我还使用正则表达式来验证输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.