Javascript->热键->禁用输入字段 [英] Javascript -> Hotkeys -> Disable for input fields

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

问题描述

好吧,所以我能使热键正常工作了

okay so I have the hotkey working just can't make it stop

   $(document).keypress(function(e){

       if(e.which == 13){
      //Enter key is press do what you want
   }
   else if(e.which == 67 || e.which == 99){
      //C key is press do what you want

      window.location.href = "/html/credits.php";

   }
    else if(e.which == 32){
        alert("Space pressed");

    }

    });

     $("input.registerform").keypress(function(e){

     e.stopPropagation(); });

这是我要使其停止的内容,我的输入表单的类是"registerform bgcolor2",但不能与"input.registerform"一起使用,也不能与"input.registerform bgcolor2"一起使用,我尝试向其中添加ID使用registerform作为ID的方法也不起作用:/

Here is what I have to make it stop, the class of my input form is "registerform bgcolor2" but it wont work with either "input.registerform" neither with "input.registerform bgcolor2" I tried adding an ID to it with registerform as ID didn't work either :/

这是引起我的AJAX的原因吗?还是我在这里想念东西?

Is it being caused my AJAX? or am I missing something here?

(很抱歉,我重新发布了这个帐户,但刚刚创建了一个新帐户,却找不到我的旧问题>.<)

(Sorry I reposted this just made a new account and cant find my old question back >.<)

推荐答案

我了解,由于您将事件侦听器附加到文档对象,因此所有输入接受元素(例如文本字段,selects等)都将处理热键,因此失去正常的行为.

I understand, that since you attach your event listener to the document object, all input accepting elements, such as textfields, selects, etc. will handle hotkeys, hence lose their normal behavior.

看看第44行 jquery.hotkeys 插件中.它在初始化时不包括所有接受输入的元素.

Take a look at line 44 in the jquery.hotkeys plugin. It excludes all input-accepting elements on initialization.

P.S.也许这个插件对于您的任务整体上很有用.

P.S. Maybe this plugin is useful as a whole for your task.

关键是检查事件是否来自接受文本的输入.

The key is to check, whether an event comes from a text-accepting input.

# only bind event to text-accepting elements, if they have been
# explicitly selected
# if your event variable happens to be called e, please adjust accordingly
if ( this !== event.target && 
    ( /textarea|select/i.test( event.target.nodeName ) ||
      event.target.type === "text") ) {
    return;
}

就目前的代码而言,您需要在匿名函数的开头插入此代码段,然后将其绑定到keypress事件.

As your code stands now, you would need to insert this snippet at the beginning of the anonymous function, you bind to the keypress event.

这篇关于Javascript->热键-&gt;禁用输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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