jquery 检查某人何时开始输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2517551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:41:01  来源:igfitidea点击:

jquery to check when a someone starts typing in to a field

jquery

提问by Drew

$('a#next').click(function() {
    var tags = $('input[name=tags]');

    if(tags.val()==''){

    tags.addClass('hightlight');  
    return false; 
    }else{
    tags.removeClass('hightlight');
    $('#formcont').fadeIn('slow');
    $('#next').hide('slow');
        return false;
    }
});

I would like the above code to fire the fadeIn as soon as somebody starts typing into the tags input. Can somebody tell me the correct way to do this or point me in the right direction? Thanks in advance

我希望上面的代码在有人开始输入标签输入时立即触发淡入淡出。有人可以告诉我这样做的正确方法或指向正确的方向吗?提前致谢

EDIT

编辑

here is the code to do it:

这是执行此操作的代码:

$('input#tags').keypress(function() {

    $('#formcont').fadeIn('slow');
    $('#next').hide('slow');
});

The only problem I've found is that my cursor no longer shows up in the text box. What am I doing wrong?

我发现的唯一问题是我的光标不再显示在文本框中。我究竟做错了什么?

采纳答案by Russell Steen

Sounds like the fade is moving your focus, hence the cursor no longer being there. Try this

听起来淡入淡出正在移动您的焦点,因此光标不再存在。尝试这个

$('input#tags').keypress(function() {

    $('#formcont').fadeIn('slow');
    $('#next').hide('slow');
    $(this).focus();
});

回答by Dead account

You want the focus event.

你想要焦点事件

  $('a#next').focus(function() {
      $('#formcont').fadeIn('slow');
  });

回答by A Rad

input#tagsis redundant and wasteful.

input#tags是多余和浪费的。

$('#tags').keypress(function() {

    $('#formcont').fadeIn('slow');
    $('#next').hide('slow');
    $(this).focus();
});