How can I automatically tab to the next field using jQuery?

前端 未结 4 1730
耶瑟儿~
耶瑟儿~ 2021-01-12 12:42

In jQuery, how can I trigger the behavior of a user tabbing to the next input field?

I\'ve tried this:

var e = jQuery.Event(\"keydown\");
e.which = 9         


        
4条回答
  •  伪装坚强ぢ
    2021-01-12 13:04

    Here's one solution, via http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/

    $.fn.focusNextInputField = function() {
        return this.each(function() {
            var fields = $(this).parents('form:eq(0),body').find(':input').not('[type=hidden]');
            var index = fields.index( this );
            if ( index > -1 && ( index + 1 ) < fields.length ) {
                fields.eq( index + 1 ).focus();
            }
            return false;
        });
    };
    

    The use is as follows:

    $( 'current_field_selector' ).focusNextInputField();
    

提交回复
热议问题