Jquery用户是否在文本字段上处于活动状态?

时间:2012-10-10 17:44:33

标签: jquery keypress

在jQuery中有很多关于keypress,keyup ......的例子。这些示例是关于在文本字段中键入的。当用户在文本字段中键入消息或其他内容时,我们可以显示User正在键入的内容,但是当用户不键入时我们无法显示有关情境的消息。以下是http://api.jquery.com/keypress/的示例:

$("#target").keypress(function() {
  $("#other).html("User is typing");
});  

<form>
  <fieldset>
    <input id="target" type="text" value="" />
  </fieldset>
</form>
<div id="other">
  Trigger the handler
</div>

如何在不输入时更改此代码并显示消息(用户未输入)?像其他选项......

编辑:这是我的想法。它与您的想法(代码)类似。谢谢你的帮助。

$(document).ready(function(){
  $("#target").keyup(function(){
      setTimeout(function(){
          $("#other").html("User is not typing");
      }, 2000);   
  });
  $("#target").keydown(function(){
    $("#other").html("User is typing");
  });
});

1 个答案:

答案 0 :(得分:1)

我建议使用延迟:

$("#target").keypress(function() {
      $("#other").html("User is typing");
      $("#other").delay(1000).queue(function(n) {
        $(this).html("User is not typing");
        n();
    });  
    });

http://jsfiddle.net/CbGL9/9/