Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 输入自动对焦类型_Javascript_Jquery_Html_Input_Autofocus - Fatal编程技术网

Javascript 输入自动对焦类型

Javascript 输入自动对焦类型,javascript,jquery,html,input,autofocus,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"

晚上好,提前谢谢大家

我有以下意见:

<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>
你们能帮帮我吗


感谢Rob

这个
内部是元素的DOM节点,因此不需要查找它
getElementById
不使用
#
,将事件绑定到其他事件中是个坏主意,因为在每次按键时都会绑定另一个事件。它们不会被覆盖,并且jQuery对象没有
.value

var leituraNim = $('#leitura_nim');
leituraNim.on('keypress', function() {
    this.contentEditable=true;  //not sure what this will do since it is an input 
    this.focus();  //If keypress is executed, shouldn't it already be focused?
}).on("keyup", function () {
   if (this.val.length == 8) {
       leituraNim.submit();
   } 
});
正如我的评论所说,按键里面的东西似乎没有意义。您要做的是侦听不在输入中的按键事件

var leituraNim = $('#leitura_nim');
leituraNim.on("keyup", function () {
   if (this.val.length == 8) {
       leituraNim.submit();
   } 
});

$(window).on("keydown", function (evt) {
    var cTarget = evt.currentTarget;  //check where event originated from
    if (cTarget.is(leituraNim)) return;  //if input, than ignore
    leituraNim.focus();  //if not than focus input
});
我试过这个,我想这就是你想要的:

HTML

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

document.onkeypress = function (e) {
    document.getElementById("leitura_nim").focus();
};

试试下面我做的。注意
的额外
#表单
ID标签。我认为您试图实现的是在输入尚未聚焦的情况下聚焦于任何按键,如果是这样,
$(文档)。on(“keypress”)
应该可以做到这一点

$(函数(){
$(文档).on(“按键”,函数(){
$(“#leitura_nim”).focus();
});
$(“#leitura_nim”)。on(“keyup”,function(){
if($(this).val().length==8){
$(“#表格”).submit();
}
});
});

这是我的荣幸
莱图拉德尼姆酒店

getElementId不使用
#
,并且leituraNim是一个jQuery对象,它没有。值我尝试了带和不带。但是我现在要删除它。您还需要将多个keyup事件绑定到元素。不要在事件中绑定事件。好的,我应该首先使用按键按钮而不是按键按钮?在输入之外,您必须将按键按钮事件绑定到窗口/文档,而不是框,因为按键按钮事件只侦听输入字段。这似乎更接近,但我认为仍然在单独的事件处理程序中使用
keyup
,与document
keypress
Thank不同,它工作得非常好,这里显示了Tyr在我帖子的评论中所说的话。当我改成文档并删除getElementsById时,它工作得很好。谢谢大家,你们一直让我想学习更多关于编程的知识。
$(document).on('keypress', function() {
  $('#leitura_nim').contentEditable = true;
  $('#leitura_nim').focus();
});

$('#leitura_nim').on('keyup', function() {
  if ($(this).val().length == 8) {
    $("#leitura-form").submit();
  }
});
document.onkeypress = function (e) {
    document.getElementById("leitura_nim").focus();
};
$(document).on("keyup", function(e) {
        document.getElementById('leitura_nim').focus();

    });