抱歉,如果这个问题已经被问过,我正在尝试创建一个应用程序,用户扫描条形码,然后数据显示在屏幕上。我不想使用文本框,因为我不希望数据可编辑。
到目前为止,我已经成功获得了在条形码末尾自动发送的回车键,但之前似乎无法按下该键,任何帮助将不胜感激!
var item = "";
$(document).keypress(function(e) {
if(e.which == 13) {
alert(item);
}else{
item = item + e.which();
}
});
只是为了澄清我想要做的是显示一个警告框,并在输入之前按下按键!例如:A B C D ENTER 将显示警报(“ABCD”)
谢谢!
更新
所以我的代码可以工作,但我得到“undefinedTHEKEYSPRESEDHERE”作为返回:
var counter = 0;
var item = [];
$(document).keypress(function(e) {
if(e.which == 13) {
alert(item[counter]);
counter++;
}else{
item[counter] = item[counter] + String.fromCharCode(e.which);
}
});
显然这还不够清楚,所以我在下面概述了我的问题:
我从alert(item[counter]);中得到什么:
undefinedKEYS
我想从alert(item[counter]);中得到什么:
KEYS
因此,从 undefinedKEYS 到 just KEYS,我需要删除文本“undefined”。
够清楚了吗?
最佳答案
将e.which()
更改为String.fromCharCode(e.which)
。
var item = "";
$(document).keypress(function(e) {
if(e.which == 13) {
alert(item);
}else{
item = item + String.fromCharCode(e.which);
}
});
关于javascript - 如何使用 jQuery 在输入之前按下按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080313/