javascript - 在类型上输入自动对焦

标签 javascript jquery html input autofocus

晚上好,提前致谢!

我有这个输入:

<div class="ref-control">
    <h3>Por Favor passe o passe o cartão pelo leitor</h3>
    <form>
        <label for="leitura-nim"> Leitura de Nim
            <input tabindex="99" autofocus type="text" name="leitura-nim" id="leitura_nim" />
        </label>
    </form>
</div>

并且此输入在页面加载时获得自动对焦,到目前为止一切顺利,我的问题是,当用户在输入外部单击时,我需要它在用户按下键盘上的键或输入时再次成为自动对焦从某些设备接收(它与测试输入相同),到目前为止我得到了这个 js,但它没有启用对按键的关注。

var leituraNim = $('#leitura_nim');

leituraNim.on('keypress', function() {
    document.getElementById('leitura_nim').contentEditable = true;
    document.getElementById('leitura_nim').focus();


});
leituraNim.on('keyup', function() {

    if (leituraNim.value.length == 8) {
        leituraNim.submit();
    }
});

你们能帮帮我吗?

谢谢罗布

最佳答案

尝试像下面这样的东西。注意额外的 #form <form id="form"> 的 ID 标签.我认为你想要实现的是在输入尚未聚焦时聚焦于任何按键,如果是这样,$(document).on("keypress")应该可以解决问题。

$(function() {
  $(document).on("keypress", function() {
    $("#leitura_nim").focus();
  });
  
  $("#leitura_nim").on("keyup", function() {
    if($(this).val().length == 8) {
      $("#form").submit();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="ref-control" >
<h3>Por Favor passe o passe o cartão pelo leitor</h3>
<form id="form">
<label for="leitura-nim"> Leitura de Nim
<input tabindex="99" autofocus type="text" name="leitura-nim" id="leitura_nim"  />
</label>
</form>
</div>

关于javascript - 在类型上输入自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30719543/

相关文章:

javascript - 从 Click 回调函数中访问 JQuery UI Widget

javascript - 匹配div的正确颜色

html - 将图像标题粘贴在图像下方,对齐 ="right"

javascript - 如何将文本区域的内容发送到 Typescript/Javascript/Html 中的函数

javascript - map 框 JS : Efficient and simple way to change a property of a feature

javascript - 如何创建动态 HTML header

javascript - 具有类似 php 语法的 node.js 模板库

javascript - Node 服务器未收到将 src 文件更改为 img 元素的请求

javascript - 如何在文本字段上加载数据?

html - 使用 bootstrap 在文本周围定位图像