Pasar el foco con la tecla enter
Publicado por Nestor (12 intervenciones) el 08/01/2019 02:28:20
Este ejemplo que me funciona solo en chrome, podras revisarlo a ver si me podes ayudar?
Este ejemplo lo que hace es saltar de campo en campo con la tecla enter, hasta el ultimo campo del tabindex y luego con un enter mas, hace el submit
****HTML****
****JavaScript****
Este ejemplo lo que hace es saltar de campo en campo con la tecla enter, hasta el ultimo campo del tabindex y luego con un enter mas, hace el submit
****HTML****
1
2
3
4
5
6
7
8
9
10
<input title="Codigo de Socio" name="tnumsoc" maxlength="8" style='width:100px' class="separadorDecimal"
tabindex="1" value="<?php echo $campos["numsoc"]; ?>" <?php if(isset($foco["numsoc"])){echo $foco["numsoc"];} ?> />
<input title="Integrante" name="tintsoc" maxlength="2" style='width:30px'
tabindex="2" value="<?php echo $campos["intsoc"]; ?>" <?php if(isset($foco["intsoc"])){echo $foco["intsoc"];} ?> />
<input title="Apellido y nombre o razon socias" name="tnomsoc" maxlength="50" style='width:590px;text-transform:uppercase;'
tabindex="3" value="<?php echo $campos["nomsoc"]; ?>" <?php if(isset($foco["nomsoc"])){echo $foco["nomsoc"];} ?> />
<input id='boton' tabindex="4" name="bacepto" type="submit" value="Aceptar" style='width:100px'>
****JavaScript****
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
document.addEventListener('keypress', function(evt) {
// Si el evento NO es una tecla Enter
if (evt.key !== 'Enter') {
return;
}
let element = evt.target;
// AQUI logica para encontrar el siguiente
let tabIndex = element.tabIndex + 1;
var next = document.querySelector('[tabindex="'+tabIndex+'"]');
// Si encontramos un elemento
if (next) {
next.focus();
event.preventDefault();
}
});
Valora esta pregunta


0