Enter key press behaves like a Tab in Javascript

后端 未结 22 1246
说谎
说谎 2020-11-27 10:52

I\'m looking to create a form where pressing the enter key causes focus to go to the \"next\" form element on the page. The solution I keep finding on the web is...

22条回答
  •  难免孤独
    2020-11-27 11:32

    This worked for me:

    $(document).on('keydown', ':tabbable', function(e) {
    
        if (e.key === "Enter") {
            e.preventDefault();
    
            var $canfocus = $(':tabbable:visible');
            var index = $canfocus.index(document.activeElement) + 1;
    
            if (index >= $canfocus.length) index = 0;
            $canfocus.eq(index).focus();
        }
    
    });
    

提交回复
热议问题