2011-12-17 23 views
5

Quiero cambiar el código clave en la tecla (presionar la tecla) en todas las entradas en una página.Quiero reemplazar Introducir código clave con código de tecla TAB. ¿Como puedo hacer esto?Cómo simular TAB en tecla ENTRAR en javascript o jQuery

gracias


EDIT 1)

Considere este código:

<div> 
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 
     <asp:ListItem>3</asp:ListItem> 
     <asp:ListItem>4</asp:ListItem> 
    </asp:RadioButtonList> 
    <br /> 
    <br /> 
    <asp:TextBox ID="TextBox1" runat="server">3333</asp:TextBox> 
    <br /> 
    <br /> 
    <asp:DropDownList ID="DropDownList1" runat="server"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 
     <asp:ListItem>3</asp:ListItem> 
    </asp:DropDownList> 
</div> 

que quiero cuando el usuario pulse Intro en eny del enfoque de control por encima de ir al siguiente control.

gracias

+2

puede encontrar pregunta similar a http://stackoverflow.com/questions/1009808/enter-key-press-behaves-like-a-tab-in- javascript – Pavan

+0

no funciona para mí – Arian

+0

Creo que vale la pena observar que cambiar el comportamiento predeterminado de los navegadores de esa manera no es una buena idea. Va en contra de lo que los usuarios esperan que suceda cuando se presiona la tecla. –

Respuesta

-1

creo que este trabajo:

$('input').live("keypress", function (e) { 
     /* ENTER PRESSED*/ 
     var OffSet = 0; 
     if (e.keyCode == 13) { 
      /* FOCUS ELEMENT */ 
      if ($(this).is("input[type='radio']")) { 
       var tblID = $(this).closest('table').attr('id'); 
       var radios = $('#' + tblID).find(":input"); 
       //alert(radios.index(this)); 
       OffSet = radios.length - radios.index(this) - 1; 
      } 
      //alert(OffSet); 

      var inputs = $(this).parents("form").eq(0).find(":input"); 
      var idx = inputs.index(this); 
      inputs[idx + OffSet].blur(); 

      try { 
       inputs[idx + OffSet].selectionStart = inputs[idx + OffSet].selectionEnd = -1; 
      } catch (e) { 

      } 
      if (idx == inputs.length - 1) { 
       inputs[0].select(); 
      } else { 
       inputs[idx + 1 + OffSet].focus(); // handles submit buttons 
       try { 
        inputs[idx + 1 + OffSet].select(); 
       } catch (e) { 
       } 
      } 
      return false; 
     } 
    }); 
2

Hope esto funciona

$('input,textarea').keydown(function(){ 
    if(event.keyCode==13) { 
    event.keyCode = 9; 
    } 
}); 

Editar

Prueba este http://jsfiddle.net/GUmUg/. Juega un poco con los selectores para hacer este trabajo, ya que no sé asp

$('input,textarea').keypress(function(e){ 
    if(e.keyCode==13) { 
    $(this).next().focus(); 
    } 
}); 
+0

No funciona para mí. ¿Lo prueba? – Arian

+0

no lo probé – aWebDeveloper

+0

por favor vea mi Editar 1 – Arian

3

Este código es para reemplazar entrar con carácter de tabulación:

$("#wmd-input").bind("keypress", function(e) { 
    if (e.keyCode == 13) { 
     var input = $(this); 
     var inputVal = input.val(); 
     setTimeout(function() { 
     input.val(inputVal.substring(0,inputVal.length) + "\t"); 
     }, 1); 
    } 
}); 

Live Demo

ACTUALIZACIÓN:

Este código se centrará en el siguiente elemento:

$(document).ready(function() { 
    $("input,select").bind("keydown", function (e) { 
     if (e.keyCode == 13) { 
      var allInputs = $("input,select"); 
      for (var i = 0; i < allInputs.length; i++) { 
       if (allInputs[i] == this) { 
        while ((allInputs[i]).name == (allInputs[i + 1]).name) { 
         i++; 
        } 

        if ((i + 1) < allInputs.length) $(allInputs[i + 1]).focus(); 
       } 
      } 
     } 
    }); 
}); 
+0

lo siento no funciona para mí. Estoy en Chrome – aWebDeveloper

+0

Supongo que entendí mal la pregunta. ¿Quieres enfocarte en otros elementos en TAB press? –

+0

No funciona. Consulte mi edición 1.Quiero centrar ir al siguiente control – Arian

1
$('input').on('keydown',function(e){ 
    var keyCode = e.keyCode || e.which; 
    if(e.keyCode === 13) { 
     e.preventDefault(); 
     $('input')[$('input').index(this)+1].focus(); 
     } 
}); 

cheque violín aquí: http://jsfiddle.net/Pd5QC/

+0

No funciona.Por favor, vea mi Editar 1 – Arian

+1

te conseguí ... tengo la actualización del código ... revisa el violín también ... –

6

He tenido un problema similar, donde quería presionar + en el teclado numérico a la pestaña al siguiente campo Ahora he lanzado una biblioteca que creo que te ayudará.

PlusAsTab: Un complemento jQuery para usar la tecla numpad plus como una tecla de tabulación equivalente.

Puesto que usted quiere entrar / su lugar, puede configurar las opciones. Averigüe qué tecla desea usar con el jQuery event.which demo.

JoelPurra.PlusAsTab.setOptions({ 
    // Use enter instead of plus 
    // Number 13 found through demo at 
    // https://api.jquery.com/event.which/ 
    key: 13 
}); 

a continuación, active la función agregando plus-as-tab="true" a los campos de formulario que desea utilizar ingrese-como-pestaña en, o algún otro elemento que contiene estos campos de formulario. Los botones de opción no deberían ser un problema, ya que están cubiertos por mi otra biblioteca, EmulateTab - vea autonavigation of radio buttons in that demo.

<div plus-as-tab="true"> 
    <!-- all focusable elements inside the <div> will be enabled --> 
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"> 
    <!-- Radio buttons should not be a problem. --> 
    </asp:RadioButtonList> 
</div> 

Puede probarlo usted mismo en el PlusAsTab enter as tab demo.

+0

Me gusta que conectes Joel, pero no parece prestarle atención a tabindex cuando utilizas la tecla enter como una pestaña. ( –

+1

@EdDeGagne: 'tabindex' es ignorado por diseño - vea [SkipOnTab versus tabindex] (https://github.com/joelpurra/skipontab/wiki/SkipOnTab-versus-tabindex). Probablemente debería escribir algunos cosa similar o enlace a esa página de PlusAsTab y EmulateTab. –

+1

¡Buen complemento de trabajo! Buena documentación. – gicalle

1

La manera en que lo hago es mediante el uso de jquery para cada una de las opciones y centrándome en el elemento después de que esté encendido.

$(document).on('keyup', '.my-input', function (ev) { 

    if (ev.keyCode == '13') { 
     var currentInput = this; 
     var isOnCurrent = false; 

     $('.my-input').each(function() { 

      if (isOnCurrent == true) { 
       $(this).focus(); 
       return false; 
      } 

      if (this == currentInput) { 
       isOnCurrent = true; 
      } 

     }); 
    } 
}); 
+0

lo siento, no funciona – Arian

0

Creé un simple jQuery plugin que no resuelve este problema. Utiliza el selector ': tabbable' de jQuery UI para encontrar el siguiente elemento 'tabbable' y lo selecciona. el uso

Ejemplo:

// Simulate tab key when enter is pressed   
$('.myElement').bind('keypress', function(event){ 
    if(event.which === 13){ 
     if(event.shiftKey){ 
      $.tabPrev(); 
     } 
     else{ 
      $.tabNext(); 
     } 
     return false; 
    } 
}); 
-1
$(document).ready(function() { 

//Objetos con CssClass="EntTab" sustituye el Enter (keycode 13) por un Tabulador (keycode 9)!! 

    $(".EntTab").bind("keypress", function(e) { 
     if (e.keyCode == 13) { 
      var inps = $("input, select"); //add select too 
      for (var x = 0; x < inps.length; x++) { 
       if (inps[x] == this) { 
        while ((inps[x]).name == (inps[x + 1]).name) { 
         x++; 
        } 
        if ((x + 1) < inps.length) $(inps[x + 1]).focus(); 
       } 
      } e.preventDefault(); 
     } 
    }); 
}); 
Cuestiones relacionadas