所以我建立一个虚拟键盘使用下列键输入和一个动态的onclick功能的主键。我希望能够按ENTER键并点击该按钮高手,但由于某种原因,它不会工作。当我按下回车键,就会点击最后一个按钮按下我
我尝试使用此功能
input.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("MasterButton").click();
}
});
但没有奏效。该系统识别按键,但它也可能只是默认。
<div id="VirtualKey" style="float:left">
<input id="btn1" type="button" onclick="input(this);" class="keypad" value="1" />
<input id="btn2" type="button" onclick="input(this);" class="keypad" value="2" />
<input id="btn3" type="button" onclick="input(this);" class="keypad" value="3" />
<br />
<input id="btn4" type="button" onclick="input(this);" class="keypad" value="4" />
<input id="btn5" type="button" onclick="input(this);" class="keypad" value="5" />
<input id="btn6" type="button" onclick="input(this);" class="keypad" value="6" />
<br />
<input id="btn7" type="button" onclick="input(this);" class="keypad" value="7" />
<input id="btn8" type="button" onclick="input(this);" class="keypad" value="8" />
<input id="btn9" type="button" onclick="input(this);" class="keypad" value="9" />
<br />
<input id="btn0" type="button" onclick="input(this);" class="keypad" value="0" />
<input id="btn." type="button" onclick="input(this);" class="keypad" value="." />
<input id="btnDel" type="button" value="DEL" onclick="del();" class="keypad"></button>
</div>
<div id="Master Input Section"><br><input type="number" step="0.1" id="Student"></input>
<div id="Master Button Section"><br><button type="submit" id="MasterButton" class="button" onclick="">Master</button></div>
<script>
function input(e) {
// Get the TextBox object.
var tbInput = document.getElementById("Student");
// As e is the button, its value is the text on it.
// Add the value to the TextBox.
tbInput.value = tbInput.value + e.value;
}
function del() {
// Get the TextBox object.
var tbInput = document.getElementById("Student");
// Remove the last char in the TextBox.
tbInput.value = tbInput.value.substr(0, tbInput.value.length - 1);
}
</script>
预期结果,输入键单击MasterButton
更新发现错误代码类型错误:input.addEventListener不是一个函数[了解更多]
我还没有使用此方法之前,因此这是奇怪的尝试。我最冲刷净试图找到一种方式来得到这个工作的很大一部分,但幸运的是我发现它的另一篇文章
<input type="number" step="0.1" id="Student" onkeydown="if (event.keyCode == 13)
document.getElementById('MasterButton').click()"></input>
希望这可以帮助别人在不久的将来。但这并绑定该输入字段添加到MasterButton回车键。请注意,我只有一个输入字段,只有1按钮的代码的其他部分工作动态