How to simulate tab key with enter key on javascri

2020-04-18 06:08发布

    <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 event when i press the enter key.but it doesn't behave as tab key pressed when i press the enter key.

please help me here..

2条回答
冷血范
2楼-- · 2020-04-18 06:41

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 :

<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.

查看更多
祖国的老花朵
3楼-- · 2020-04-18 06:49

return event.keycode is effectively return 9, and even return event will 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 enter event and then manually change focus to the next required field:

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()
      }
    });
  });
}
查看更多
登录 后发表回答