将输入键映射到HTML/CSS/JS中的按钮

问题描述:

我有一个小表单,您必须提交一个分数,这是一个数字,然后我有一个处理数字的脚本。将输入键映射到HTML/CSS/JS中的按钮

的形式是像这样:

<form name ="input2" style="margin-left: 30%; margin-right: 30%; margin-bottom:5%"> 
 
    Score: 
 
    <input type="text" id="score" name="score" maxlength="3" onkeydown="if(event.keyCode==13) changePlayer();" patern="\d"><br> 
 
    <input id = "submit" type="button" value="Submit" name="Submit" class="btn btn-primary" onclick="changePlayer(); return false"/> 
 
    <input type="button" name="Cancel" class="btn btn-primary" value="Cancel"/> 
 
</form>

我在文本框中输入号码,然后当我点击提交按钮,该功能的进程数。事情是,当我按下回车键时,我想要发生同样的事情,但相反,页面刷新,整个事情重新开始,我失去了数据。

我怎样才能将输入键映射到按钮或函数,而不刷新页面?

只需将<input id = "submit" type="button"更换为<input id = "submit" type="submit"即可。你不需要检查。

function changePlayer(score) { 
 
    alert(input2.score.value); 
 
}
<form name="input2" style="margin-left: 30%; margin-right: 30%; margin-bottom:5%" onsubmit="changePlayer();return false;"> 
 
    Score: 
 
    <input type="text" id="score" name="score" maxlength="3" patern="\d"><br> 
 
    <input id = "submit" type="submit" value="Submit" name="Submit" class="btn btn-primary" onclick="changePlayer(); return false"/> 
 
    <input type="button" name="Cancel" class="btn btn-primary" value="Cancel"/> 
 
</form>

+0

我相信它的工作,谢谢 – Reaper9806