简体   繁体   English

一次输入 keypress 运行 JavaScript keypress 事件处理程序代码两次

[英]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.我试过keypresskeydownevent.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.这是一个问题,因为这样变量fimsegseguinte以错误的值开始该循环。

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:根据您的评论,我认为主要问题是:

  • It sounds like this is meant to be some sort of client-side validation script.这听起来像是某种客户端验证脚本。 But you don't explain exactly what it is supposed to do if the input is valid, or invalid.但是,如果输入有效或无效,您并没有准确解释它应该做什么。 I'm going to assume that you want a popup message either way.我将假设您希望以任何一种方式弹出消息。 I think it makes more sense to separate the "jump to the next empty box" behavior from the "validate and submit the values" behavior.我认为将“跳转到下一个空框”行为与“验证并提交值”行为分开更有意义。 So I did that in two separate handlers.所以我在两个单独的处理程序中做到了这一点。
  • If you focus on the button (eg by tabbing to it, or pressing it down but moving the mouse away before releasing the mouse button) and then press Enter, you will get an error because the ID of the button does not match the expected pattern of the ID's you've set for the text boxes.如果您将注意力集中在按钮上(例如,通过使用 Tab 键,或按下它但在释放鼠标按钮之前将鼠标移开)然后按 Enter,您将收到错误消息,因为按钮的 ID 与预期的模式不匹配您为文本框设置的 ID。 So I changed the button to act the same as an Enter keypress on the first (top-left text box.) And I called event.stopPropagation() to prevent it from bubbling up to the document Enter handler.因此,我将按钮更改为与第一个(左上角文本框)上的 Enter 按键相同。并且我调用了event.stopPropagation()以防止它冒泡到文档 Enter 处理程序。
  • I updated the Enter handler to only work on the textboxes, because you would get an error if you focus on just the document body or the button and then press Enter.我将 Enter 处理程序更新为仅适用于文本框,因为如果您只关注文档正文或按钮,然后按 Enter,则会出现错误。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM