我试图仅将输入字段限制为字符,如SO post中所述。 javascript工作得很好,但正如此处和其他地方所述,当按下shift键时它不起作用。所以我可以按住Shift键,键入5和'%'出现。所以我添加了这段代码,只返回event.shiftKey = true
的keydown事件sort
当我设置断点并使用chrome中的调试器时,这是有效的。但是,它不能实时工作,即使按住shift键,也会调用numbersOnly(event)函数。
为什么函数会在调试时返回,但不能实时返回?
答案 0 :(得分:1)
Following code works fine
$(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>