Jquery select NEXT text field on Enter Key Press

后端 未结 6 1457
感动是毒
感动是毒 2020-12-31 15:31

I have made a page using jquery, and on load it selects the first text field automatically. I want it to then move to the next field when the ENTER key is pressed.



        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 15:50

    try this:

    (function($){
        $.fn.enterNext = function(){
           var _i =0;
           $('input[type=text], select',this)
                .each(function(index){
                _i = index;
                $(this)
                    .addClass('tab'+index)
                    .keydown(function(event){
                        if(event.keyCode==13){
                            $('.tab'+(index+1)).focus();
                            event.preventDefault();
                        }
                    });
    
            })
         $( "input[type=submit]",this ).addClass('tab'+(_i+1));
    }})(jQuery);
    

    for use:

    $('form.element').enterNext();
    

    in my case this is the best solution in that I got because the function .next() is strict with elements outside their branch DOM. The best way is to force an index.

    and sorry for my bad English...

提交回复
热议问题