【发布时间】:2015-08-10 06:21:23
【问题描述】:
我想检测输入中的 Control + A 事件。我可以找到 Control + A 事件,但即使在return false 之后该功能仍在继续。
jsFiddle - http://jsfiddle.net/f6rcgpmh/4/
$('.searchTerm').keyup(function(e) {
$("#status").text("");
if (e.ctrlKey) {
if (e.keyCode == 65 || e.keyCode == 97) { // 'A' or 'a'
console.log("Control pressed");
e.preventDefault();
return false;
}
}
$("#status").text("This should not work if Ctrl + A is pressed");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="search" class="search">
<input class="searchTerm" placeholder="Filter Books...">
<input class="searchButton" type="submit">
</form>
<div id="status"></div>
我希望它在 keyup 而不是 keydown 中工作。因为我正在使用自动搜索,我不想在 keyrelease 之前调用函数。而且 Ctrl + A 在返回 false 时不会突出显示 keydown 中的文本。
【问题讨论】:
-
control + a 在你的浏览器中有什么作用?
-
它打印文本......它没有破坏功能
-
keydown在keyup之前触发,对吗? -
这是因为当你释放最后一个键时,要么没有
ctrlKey,要么keyCode不再是a或A -
@TheWarlock : yupe..check jsfiddle.net/f6rcgpmh/7 .not working for single letter.
标签: javascript jquery