2011-12-29 27 views
6

La tecla Intro debe funcionar como una tecla de tabulación presionada. La tecla Intro para TextArea y el botón Enviar deben funcionar como siempre. El foco debe saltarse de la siguiente elemento cuando el siguiente campo está desactivado/solo de lectura.Cómo convertir una tecla Intro Pulse en una pestaña Presione la tecla para páginas web

gracias,

+0

posible duplicado de [Enter pulsación de tecla se comporta como una pestaña en Javascript] (http: // stackoverflow .com/questions/1009808/enter-key-press-behaves-like-a-tab-in-javascript) –

+0

Buena combinación de candidatos, ya que esta pregunta requiere soluciones jQuery específicamente. – xan

+0

respuesta para Andrew Whitaker es realmente agradable y no está fundada en otra pregunta similar o relacionada. –

Respuesta

5

En primer lugar, esto probablemente no es una gran idea usability-wise. Sin embargo, aquí hay algo que debería funcionar:

$(":input").on("keydown", function(event) { 
    if (event.which === 13 && !$(this).is("textarea, :button, :submit")) { 
     event.stopPropagation(); 
     event.preventDefault(); 

     $(this) 
      .nextAll(":input:not(:disabled, [readonly='readonly'])") 
      .first() 
      .focus(); 
    } 
}); 

Ejemplo:http://jsfiddle.net/NDcrk/

La pieza que se encuentra la siguiente entrada puede tener que cambiar, dependiendo de su margen de beneficio.

0

Tuve un problema similar, donde quería presionar + en el teclado numérico para desplazarme 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 
}); 

HTML de ejemplo

<!-- Enable enter as tab as the default for all fields in this form --> 
<form data-plus-as-tab="true"> 
    <input type="text" value="Enter skips to the next field" autofocus="autofocus" /> 
    <!-- Exclude this textarea --> 
    <textarea data-plus-as-tab="false">Enter works as usual</textarea> 
    <input type="text" value="Enter skips to the next field" /> 
    <select><option>Enter skips here too</option></select> 
    <!-- Exclude this submit button --> 
    <button type="submit" data-plus-as-tab="false">Enter works as usual</button> 
</form> 

Como se puede ver, es fácil para que todos los elementos en un recipiente con data-plus-as-tab="true", y es fácil para excluir elementos específicos con data-plus-as-tab="false". También puede excluir ciertos tipos (en elementos existentes) de javascript con $('textarea, :button').plusAsTab(false);.

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

0

Esta solución funciona para mí. Probado en IE y FireFox.

<script type="text/javascript"> 
    function tabE(obj, e) { 
     var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz 
     if (e.keyCode == 13) { 
      var ele = document.forms[0].elements; 
      for (var i = 0; i < ele.length; i++) { 
       var q = (i == ele.length - 1) ? 0 : i + 1; // if last element : if any other 
       if (obj == ele[i]) { 
        ele[q].focus(); 
        break 
       } 
      } 
      return false; 
     } 
    } 
</script> 

<form METHOD="POST"> 
    <input name="" type="text" onkeypress="return tabE(this,event)"> 
    <br> 
    <input name="" type="text" onkeypress="return tabE(this,event)"> 
    <br> 
    <input name="" type="text" onkeypress="return tabE(this,event)"> 
    <br> 
    <INPUT TYPE="submit" Value="Ok"> 
</form> 

Lo encontré here.

+0

Bienvenido a [stackoverflow.com] (http://stackoverflow.com), gracias por su ayuda. Por favor, tenga en cuenta para describir su respuesta también. – hofmeister

0

¿Cómo comprobar el elemento del formulario se muestra en este caso? Tengo muchos entrada (cuadro de texto, botón de radio,) pero se muestran algunos elementos, algunos elementos no se muestran

<script type="text/javascript"> 
function tabE(obj, e) { 
var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz 
if (e.keyCode == 13) { 
var ele = document.forms[0].elements; 
for (var i = 0; i < ele.length; i++) { 
var q = (i == ele.length - 1) ? 0 : i + 1; // if last element : if any other 
if (obj == ele[i]) { 
ele[q].focus(); 
break 
} 
} 
return false; 
} 
} 
</script> 

<form METHOD="POST"> 
<input name="" type="text" onkeypress="return tabE(this,event)"> 
<br> 
<input name="" type="text" onkeypress="return tabE(this,event)"> 
<br> 
<input name="" type="text" onkeypress="return tabE(this,event)"> 
<br> 
<INPUT TYPE="submit" Value="Ok"> 
</form> 
+0

Bienvenido a StackOverflow. Si tiene alguna pregunta, por favor haga una nueva pregunta – slfan

Cuestiones relacionadas