简单的Javascript登录不提交

时间:2016-09-01 15:56:48

标签: javascript jquery html

我有以下Javascript:

function TheLogin() {

var password = 'mysg';

if (this.document.login.pass.value == password) {
  top.location.href="main.html";
}
else {
  window.alert("Incorrect password, please try again.");
  }
}

以及以下HTML:

<form name="login" style="margin: 5px 0px 0px 0px;">
<input type="password" name="pass" size="30" onkeydown="if(event.keyCode==13) return false;" style="width: 130px;"><br>
<input type="button" class="button" value="Login" style="width: 134px; margin: 4px auto 4px auto;" onclick="javascript:TheLogin(this.form)">

我需要做两件事:
  1.当用户点击按钮时,javascript会执行,因此&#34;将它们记录在&#34;
中   2.当用户按下Enter键时,它会提交按钮并在&#34;

中记录它们

绝望让这个工作!! TIA!

1 个答案:

答案 0 :(得分:0)

使用以下内容修复:

<script>
function validateForm() {
    var x = document.forms["myForm"]["fname"].value;
    if (x != "password") {
        alert("Incorrect password. Please try again!");
        return false;
    }
}
</script>

<form name="myForm" action="main.html" onsubmit="return validateForm()" method="post">
Password: <input type="password" name="fname">
<br>
<input type="submit" value="Submit" class="button">
</form>