如何确保用户在 jquery 中按下“可打印”字符?
我试试这个,但它不起作用
$("#foo").keyup(function (e) {
if (e.charCode) {
console.debug('this is printable char');
}
});
我只想要数字、AZ az 字符以及“è,$,%”等,但不想要箭头、输入、f5 等
尝试:
$("#foo").keyup(function (e) {
if (e.which < 0x20) {
console.debug('this is not printable char');
return;
}
else {
console.debug('this is printable char');
}
});
<script type="text/javascript">
$("input").keypress(function (e) {
if (e.which !== 0 && e.charCode !== 0) {
alert(String.fromCharCode(e.keyCode|e.charCode));
}
});
</script>
似乎拖曳与 jQuery 1.4.2、FF、IE、Chrome 一起工作得很好。
要深入研究 JS 键盘事件处理的混乱,请参阅: JavaScript Madness: Keyboard Events