JavaScript ->热键 ->禁用输入字段

发布于 2024-11-29 07:50:37 字数 707 浏览 5 评论 0原文

好吧,我的热键工作只是不能让它停止

   $(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”我尝试使用registerform向其添加ID,因为ID也不起作用:/

这是由我的AJAX引起的吗?或者我在这里遗漏了什么?

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

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(); });

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 :/

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 >.<)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃繁缕 2024-12-06 07:50:37

据我所知,由于您将事件侦听器附加到文档对象,因此所有输入接受元素(例如文本字段、选择等)都将处理热键,因此会失去其正常行为。

看一下 第 44 行 中的 <一个 href="https://github.com/jeresig/jquery.hotkeys" rel="nofollow">jquery.hotkeys 插件。它在初始化时排除所有接受输入的元素。

PS 也许这个插件作为一个整体对您的任务很有用。

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

# 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;
}

正如您现在的代码所示,您需要在绑定到按键事件的匿名函数的开头插入此代码片段。

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.

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

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;
}

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.

魂ガ小子 2024-12-06 07:50:37

似乎工作得很好:)

示例:
第一个例子: http://jsfiddle.net/HenryGarle/SG5Um/
第二个示例: http://jsfiddle.net/HenryGarle/SG5Um/1/

新代码:

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

   if(e.which == 13){
        alert("Enter");
   }
   else if(e.which == 67 || e.which == 99){
        alert("c");
       //window.location = 'whateveryouwant';
   }
    else if(e.which == 32){
        alert("Space pressed");
    }

});

$("input.registerform.bgcolor2").live('keypress', function(e){
    alert("Stopped");
    e.stopPropagation();
});

Stops:
<input class="registerform bgcolor2" type="text">
<br>
Does not stop:
<input class="registerform" type="text">

仅使用寄存器形式使用此任何内容都会正常运行,但如果它也有 bgcolor2 它将停止事件。

Seems to be working just fine :)

example:
First example: http://jsfiddle.net/HenryGarle/SG5Um/
Second example: http://jsfiddle.net/HenryGarle/SG5Um/1/

New code:

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

   if(e.which == 13){
        alert("Enter");
   }
   else if(e.which == 67 || e.which == 99){
        alert("c");
       //window.location = 'whateveryouwant';
   }
    else if(e.which == 32){
        alert("Space pressed");
    }

});

$("input.registerform.bgcolor2").live('keypress', function(e){
    alert("Stopped");
    e.stopPropagation();
});

Stops:
<input class="registerform bgcolor2" type="text">
<br>
Does not stop:
<input class="registerform" type="text">

Using this anything with ONLY registerform will act as normal but if it ALSO has bgcolor2 it will stop the event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文