Pergunta

I have a PDF file that is downloadable after typing a password. If the password is wrong it redirects the user to another page called incorrect.html.

The problem is that it only works when you click the Submit button. When you press the Enter Key it doesn't work

How can this be fixed?

<form name="login">
<input class="gen-label" type="text" name="pass" size="17" onKeyDown="if(event.keyCode==13) event.keyCode=9;">
<input class="button submit" type="button" value="Submit"  onClick="TheLogin(this.form)">
</form>

Javascript

function TheLogin() {
var password = 'pass1';
if (this.document.login.pass.value == password) {
  top.location.href="path_to_PDF.pdf";
}
else {
  location.href="incorrect.html";
  }
}

Fiddle http://fiddle.jshell.net/HKLma/

Foi útil?

Solução

replace

onKeyDown="if(event.keyCode==13) event.keyCode=9;"

with

onkeyup="if(event.which === 13) TheLogin(this.form);"

or replace all the inline JS with

$('[name="login"]').on('submit', function() {
    var password = 'pass1';
    if ( $('[name="pass"]').val() == password) {
        top.location.href="path_to_PDF.pdf";
    } else {
        location.href="incorrect.html";
    }
});

Outras dicas

try this:

$('.gen-label').keyup(function(e){
if(e.keyCode == 13)
{
    $(this).trigger(".button submit");
}});

Working Demo

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top