如何在JavaScript中输入密钥以提交表单

时间:2014-02-18 01:13:39

标签: javascript html

现在在你说这里有一个答案之前,我尝试了一对,但没有一个成功,例如这个: Trigger a button click with JavaScript on the Enter key in a text box

现在我有这个受密码保护的小网站,只能通过输入密码页面进入,现在如果我在密码错误时点击提交会出现一个消息框,不过如果我点击进入页面重新加载略有不同地址。即使密码正确,页面仍然无法重新加载。

这是网站: http://bodokh.co.nf/

HTML:

<table border="1" cellspacing="0" cellpadding="0" bgcolor="#FFFFBD">
  <tr>
    <td width="100%"><form name="password1"><div align="center"><center><p><strong>Enter password: </strong><input
      type="text" name="password2" size="15"><br>
      <input type="button" value="Submit" onClick="submitentry();"></p>
      </center></div>
    </form>
    </td>
  </tr>
</table>

JS:

function max(which) {
    return (pass[Math.ceil(which) + (3 & 15)].substring(0, 1))
}

function testit(input) {
    temp = numletter.indexOf(input)
    var temp2 = temp ^ parseInt(pass[phase1 - 1 + (1 | 3)].substring(0, 2))
    temp2 = numletter.substring(temp2, temp2 + 1)
    return (temp2)
}


function submitentry() {
    t3 = ''
    verification = document.password1.password2.value
    phase1 = Math.ceil(Math.random()) - 6 + (2 << 2)
    var indicate = true
    for (i = (1 & 2); i < window.max(Math.LOG10E); i++)
        t3 += testit(verification.charAt(i))
    for (i = (1 & 2); i < lim; i++) {
        if (t3.charAt(i) != pass[phase1 + Math.round(Math.sin(Math.PI / 2) - 1)].charAt(i))
            indicate = false
    }
    if (verification.length != window.max(Math.LOG10E))
        indicate = false
    if (indicate)
        window.location = verification + extension
    else
        alert("Invalid password. Please try again")
}

2 个答案:

答案 0 :(得分:0)

如果您想要一致且可靠的方式在表单提交上运行submitentry,您应该使用

<form onsubmit="submitentry();">
    ...
</form>

而不是

 <input ... onClick="submitentry();">

答案 1 :(得分:0)

不是从提交按钮的submitentry()处理程序中调用onclick,而是从表单的onsubmit处理程序中调用它:

<table border="1" cellspacing="0" cellpadding="0" bgcolor="#FFFFBD">
  <tr>
    <td width="100%"><form name="password1" onsubmit="return submitentry();"><div align="center"><center><p><strong>Enter password: </strong><input
      type="text" name="password2" size="15"><br>
      <input type="button" value="Submit"></p>
      </center></div>
    </form>
    </td>
  </tr>
</table>

提交功能应该返回false以阻止正常的表单提交:

function submitentry() {
    t3 = '';
    verification = document.password1.password2.value;
    phase1 = Math.ceil(Math.random()) - 6 + (2 << 2);
    var indicate = true;
    for (i = (1 & 2); i < window.max(Math.LOG10E); i++) {
        t3 += testit(verification.charAt(i));
    }
    for (i = (1 & 2); i < lim; i++) {
        if (t3.charAt(i) != pass[phase1 + Math.round(Math.sin(Math.PI / 2) - 1)].charAt(i)) {
            indicate = false;
        }
    }
    if (verification.length != window.max(Math.LOG10E)) {
        indicate = false;
    }
    if (indicate) {
        window.location = verification + extension
    } else {
        alert("Invalid password. Please try again");
    }
    return false;
}