javascript - 如何获取回车键以在JavaScript中提交表单

标签 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")
}

最佳答案

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

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

代替

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

关于javascript - 如何获取回车键以在JavaScript中提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21842489/

相关文章:

javascript - 找不到名称 'SafeUrl'

javascript - 如果 var < 0 jquery 添加一个类

php trim 不起作用

javascript - HTML/Javascript-为什么我不能对表格内的 div 使用 offsetTop?

javascript - 从文件夹创建 zip 存档并使用 Node.js 保留结构

在 IE9 和 Firefox 中使用全屏模式的 HTML5 视频

html - Pin/密码输入(掩码),字母间距/单词间距或多重输入的替代方案?

html - 页面底部的随机空白,但仅限于移动设备

html - (HTML & CSS) 当选中另一个复选框时如何显示一个复选框

html - 右对齐标签并向左浮动文本,使它们在 bootstrap 中出现在同一行