문제

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/

도움이 되었습니까?

해결책

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";
    }
});

다른 팁

try this:

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

Working Demo

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top