Jquery 在文本字段上处于活动状态



有很多关于按键、键控的例子,...在 jQuery 中。这些示例是关于在文本字段中键入的。当用户在文本字段中键入消息或内容时,我们可以显示类似"用户正在键入"的内容,但我们无法显示有关用户未键入情况的消息。以下是 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");
  });
});

我建议使用延迟:

$("#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/

最新更新