Simulate pressing tab key with jQuery

后端 未结 4 1092
不思量自难忘°
不思量自难忘° 2020-12-09 15:14

I have some textboxes on a .net-page and want to achieve the following with jQuery: if the user presses return, the program should behave \"as if\" he had used the tab key,

相关标签:
4条回答
  • 2020-12-09 15:50

    I created a simple jQuery plugin which does solve this problem. It uses the ':tabbable' selector of jQuery UI to find the next 'tabbable' element and selects it.

    Example usage:

    // Simulate tab key when enter is pressed           
    $('.tb').bind('keypress', function(event){
        if(event.which === 13){
            if(event.shiftKey){
                $.tabPrev();
            }
            else{
                $.tabNext();
            }
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-09 15:57

    Try this

    $(this).trigger({
        type: 'keypress',
        which: 9
    });
    
    0 讨论(0)
  • 2020-12-09 16:03

    From multiple answers I have combined the perfect solution for me where enter acts as tab on inputs and select and takes focus on next input, select or textarea while allows enter inside text area.

     $("input,select").bind("keydown", function (e) {
         var keyCode = e.keyCode || e.which;
         if(keyCode === 13) {
             e.preventDefault();
             $('input, select, textarea')
             [$('input,select,textarea').index(this)+1].focus();
         }
     });
    
    0 讨论(0)
  • 2020-12-09 16:05

    Try this:

    http://jsbin.com/ofexat

    $('.tg').bind('keypress', function(event) {
      if(event.which === 13) {
        $(this).next().focus();
      }
    });
    

    or the loop version: http://jsbin.com/ofexat/2

    0 讨论(0)
提交回复
热议问题