<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).keypress(
function(event){
if (event.which == '13') {
event.preventDefault();
$("#j").focus();
}
});
</script>
</head>
<body>
<form id = "form1" action ='<?php echo $_SERVER['PHP_SELF']; ?>' method = "post">
<input type = "text" name = 'i' id = 'i' autofocus>
<input type = "text" name = 'j' id = 'j'>
<input type = "submit" name = 'submit' value = 'submit'>
</form>
</body>
</html>
<?php
echo $_POST['i'];
echo $_POST['j'];
?>
在上面的代码中,我最初禁用了 Enter 按键事件。我正在使用已经给出回车符的条形码扫描仪。这让我可以在使用条形码扫描仪扫描第一个输入字段后将注意力集中到下一个输入字段。但是,我希望在通过扫描仪输入第二个字段的数据时提交表单。我该如何实现这一目标?
最佳答案
首先将oninput函数添加到id为j的输入标签
<input type = "text" name = 'j' id = 'j' oninput="clickSubmitButton()">
然后提供id来提交按钮。
<input type = "submit" name = 'submit' id='submitbutton' value = 'submit'>
最后添加javascript代码以自动点击按钮。
function clickSubmitButton(){
document.getElementById("submitbutton").click(); //id of the submit button to be clicked
}
关于javascript - 在 JavaScript 中禁用和启用 Enter 按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37965770/