在输入或textarea字段中禁用jquery keypress [英] Disabling jquery keypress when in input or textarea fields

查看:66
本文介绍了在输入或textarea字段中禁用jquery keypress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在按下某个键时触发一个事件.此代码可以正常工作:

I'm trying to fire off an event when a certain key is pressed. It works fine with this code:

$(document).keypress(function(e){ 
    switch(e.which){ 
            case 96:$("a.quicklinks").trigger('click'); 
            break; 
    } 

但是,如果用户在表单字段中,我想禁用此行为.如何测试用户是否在表单字段中键入此内容?谢谢.

But, if the user is in a form field, I'd like to disable this behavior. How do I test whether the user is typing in a form field to do this? Thanks.

推荐答案

$(document).keypress(function(e) { 
    if ($(e.target).is('input, textarea')) {
        // event invoked from input or textarea
    }
    ...        
});

这篇关于在输入或textarea字段中禁用jquery keypress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆