0

I have this code

<form name="frmDiagnosticos" id="frmDiagnosticos" method="post" action="agregarDiagnosticoSesion.jsp">
  <label for="txtCodigoCIE10" class="clase">Código CIE-10</label><input class="codigoCIE10" type="text" name="txtCodigoCIE10" id="txtCodigoCIE10" />
  <label for="linkAbrirBuscador" class="clase">Buscar código CIE-10</label><button id="linkAbrirBuscador">Buscar diagnósticos</button>
  <label for="chkCronica" class="clase">Enfermedad crónica</label><input type="checkbox" name="chkCronica" id="chkCronica" />
  <label for="chkHabito" class="clase">Es un hábito</label><input type="checkbox" name="chkHabito" id="chkHabito" />
  <input type="submit" name="btnConfirmarYContinuar" value="Confirmar" id="btnConfirmarYContinuar" />
</form>

When I am focused on the first text input (ID: txtCodigoCIE10) and I press Enter, I want to trigger the event of the submit, not the button (ID: linkAbrirBuscador). How can I do?

I am using jQuery 1.11.

udondan
  • 48,050
  • 17
  • 168
  • 161

1 Answers1

2

You could do like this :

$('#txtCodigoCIE10').keydown(function(e){
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if(key == 13) 
    {
        e.preventDefault();
        $('form').submit();
    }
});
sdespont
  • 13,371
  • 7
  • 52
  • 89