我正在尝试将输入字段限制为仅在此 SO post 中记录的字符. javascript 工作得很好,但正如这里和其他地方所指出的那样,当按下 shift 键时它不起作用。这样我就可以按住 shift 键,输入 5,然后会出现一个“%”。所以我添加了这段代码来简单地返回 event.shiftKey = true 的 keydown 事件
$(document).on("keydown", ".numbers-only", function (event) {
if(event.shiftKey){
return
}
numbersOnly(event);
});
当我在 chrome 中设置断点并逐步执行调试器时,这会起作用。但是,它不是实时工作的,即使按住 shift 键也会调用 numbersOnly(event) 函数。
为什么函数在调试时会按预期返回,但不是实时返回?
最佳答案
下面的代码工作正常
$(function() {
$(document).on("keypress", function(event) {
showChar(event)
});
function showChar(e) {
if (e.shiftKey) {
return
}
console.log(String.fromCharCode(e.charCode) + ": call function numbersOnly(e)");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<p>Press any character key, with or without holding down the SHIFT key.</p>
</div>
关于javascript - event.shiftKey 在调试器中工作但不是全速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49479479/