javascript 如何在javascript上用回车键模拟tab键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17482036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:29:14  来源:igfitidea点击:

How to simulate tab key with enter key on javascript

javascriptjquerykeypressenter

提问by sanzy

    <script type="text/javascript">
        function onDataBound(e) {
            $("#batchgrid").on("click", "td", function (e) {

                $("input").on("keydown", function (event) {
                    if (event.keyCode == 13) {

                        event.keycode=9;
                        return event.keycode;
                    }
                });
            });
        }
    </script>

here i'm using above script to fire tab key press eventwhen i press the enter key.but it doesn't behave as tab key pressed when i press the enter key.

在这里,我使用上面的脚本在我按下Enter 键时触发Tab 键按下事件。但是当我按下Enter 键时,它的行为不像按下 Tab 键。

please help me here..

请在这里帮助我..

回答by mishik

return event.keycodeis effectively return 9, and even return eventwill not help, as returning the event does not mean that will be handled properly, what you probably want to do instead is to take the enterevent and then manually change focus to the next required field:

return event.keycode是有效的return 9,甚至return event无济于事,因为返回事件并不意味着会得到正确处理,您可能想要做的是获取enter事件,然后手动将焦点更改为下一个必填字段:

function onDataBound(e) {
  $("#batchgrid").on("click", "td", function (e) {
    $("input").on("keydown", function (event) {
      event.preventDefault();
      if (event.keyCode == 13) {
        $(this).next("input, textarea").focus()
      }
    });
  });
}

回答by sanyam jain

It will not simulate until you prevent the default enter key event. event.preventDefault(); should be the first command of your function.Then implement the tab key event.Your code should be something like this :

除非您阻止默认的输入键事件,否则它不会模拟。event.preventDefault(); 应该是你的函数的第一个命令。然后实现tab键事件。你的代码应该是这样的:

<script type="text/javascript">
    function onDataBound(e) {
        $("#batchgrid").on("click", "td", function (e) {

            $("input").on("keydown", function (event) {
                event.preventDefault();
                if (event.keyCode == 13) {

                    event.keycode=9;
                    return event.keycode;
                }
            });
        });
    }
</script>

Hope it will work.

希望它会起作用。