JavaScript ->热键 ->禁用输入字段
好吧,我的热键工作只是不能让它停止
$(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,由于您将事件侦听器附加到文档对象,因此所有输入接受元素(例如文本字段、选择等)都将处理热键,因此会失去其正常行为。
看一下 第 44 行 中的 <一个 href="https://github.com/jeresig/jquery.hotkeys" rel="nofollow">jquery.hotkeys 插件。它在初始化时排除所有接受输入的元素。
PS 也许这个插件作为一个整体对您的任务很有用。
关键是检查事件是否来自接受文本的输入。
正如您现在的代码所示,您需要在绑定到按键事件的匿名函数的开头插入此代码片段。
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.
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.
似乎工作得很好:)
示例:
第一个例子: http://jsfiddle.net/HenryGarle/SG5Um/
第二个示例: http://jsfiddle.net/HenryGarle/SG5Um/1/
新代码:
仅使用寄存器形式使用此任何内容都会正常运行,但如果它也有 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:
Using this anything with ONLY registerform will act as normal but if it ALSO has bgcolor2 it will stop the event.