0

所以我有一个文本字段,用户必须在其中输入密码才能下载 PDF 文件。如果密码正确,它会将用户重定向到 PDF 文件 url,如果密码错误,它会将用户重定向到另一个名为不正确 (incorrect.html) 的页面。

一切正常。唯一的问题是当您按下 Enter 键时,它不会重定向页面。它仅在您单击“提交”按钮时有效。

有没有办法来解决这个问题?

非常感谢!

输入文本字段和提交按钮

<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

<script>
function TheLogin() {
var password = 'Pass1';
if (this.document.login.pass.value == password) {
  top.location.href="file_to_download.pdf";
}
else {
  location.href="incorrect.html";
  }
}
</script>
4

1 回答 1

1

<input type="submit"在输入时触发表单提交,按钮不会。

另一件事是将 else 代码更改为如下所示的重定向。

<input class="gen-label" type="text" name="pass" size="17" onKeyDown="if(event.keyCode==13) event.keyCode=9;">
<input class="button submit" type="submit" value="Submit"  onClick="TheLogin(this.form)">
</form><script>
function TheLogin() {
var password = 'Pass1';
if (this.document.login.pass.value == password) {
  top.location.href="file_to_download.pdf";
}
else {
  window.location="incorrect.html";
  }
}
于 2014-04-04T09:53:31.657 回答