JQuery - focus触发目标输入中的keyup事件

时间:2022-11-20 00:03:47

I will demonstrate my problem using the simple example. Consider the following script

我将使用简单的示例演示我的问题。请考虑以下脚本

   $(document).on('keydown','#input1', function(e) 
   {        
      if(e.keyCode==13){ $("#input2").focus(); }    
   });

and HTML

和HTML

   <input type="text" id="input1"/>
   <input type="text" id="input2" onkeyup='alert("UP")'/>

every time I press enter in the first input, focus goes to the second input but keyup event is triggered also. I tried stopPropagation but it does not work. How can I prevent that issue?

每当我按下第一个输入时,焦点转到第二个输入,但也会触发键盘事件。我试过stopPropagation但它不起作用。我该如何预防这个问题?

1 个解决方案

#1


2  

I'd say you could use keyup instead of keydown

我会说你可以使用keyup而不是keydown

$(document).on('keyup','#input1', function(e) 
{        
    if(e.keyCode==13){ $("#input2").focus(); }    
});

jsFiddle Demo

jsFiddle演示

#1


2  

I'd say you could use keyup instead of keydown

我会说你可以使用keyup而不是keydown

$(document).on('keyup','#input1', function(e) 
{        
    if(e.keyCode==13){ $("#input2").focus(); }    
});

jsFiddle Demo

jsFiddle演示