Enter key press behaves like a Tab in Javascript

后端 未结 22 1244
说谎
说谎 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:40

    You can use my code below, tested in Mozilla, IE, and Chrome

       // Use to act like tab using enter key
        $.fn.enterkeytab=function(){
             $(this).on('keydown', 'input, select,', function(e) {
            var self = $(this)
              , form = self.parents('form:eq(0)')
              , focusable
              , next
              ;
                if (e.keyCode == 13) {
                    focusable = form.find('input,a,select,button').filter(':visible');
                    next = focusable.eq(focusable.index(this)+1);
                    if (next.length) {
                        next.focus();
                    } else {
                        alert("wd");
                        //form.submit();
                    }
                    return false;
                }
            });
    
        }
    

    How to Use?

    $("#form").enterkeytab(); // enter key tab

提交回复
热议问题