我在 iPad 上用 JavaScript 记录击键时遇到问题.以下脚本适用于 Chrome 和 Safari,但不适用于 iPad Safari.蓝牙条码扫描器发送 12 位数字作为按键,然后发送一个返回字符.有人有什么想法吗?
I'm having trouble logging keystrokes in javascript on the iPad. The following script works on Chrome and Safari, but not iPad Safari. The bluetooth barcode scanner sends 12 digits as keystrokes, then sends a return character. Does anyone have any ideas?
我想你需要一个 iPad 来试试这个 :)
I think you will need an iPad to try this out :)
谢谢,标记
$(document).ready(function(){
$(document).keypress(function(e){
if( e.keyCode == 13){
alert($('#barcode').attr('value'));
$('#barcode').attr('value','');
}
else{
var key = String.fromCharCode(e.which);
var new_val = $('#barcode').attr('value') + key;
$('#barcode').attr('value',new_val);
}
});
});
iOS 版 Safari 不会在非表单组件的 DOM 元素上触发键盘事件.这包括通常用于捕获页面上任何位置的击键的文档和正文.
Safari for iOS doesn't trigger keyboard events on DOM elements that are not components of a form. This includes the document and body which are usually used to capture keystrokes anywhere on the page.
在文档或页面正文上触发击键事件的唯一方法是在输入或文本区域中触发它.在这种情况下,事件将正确地冒泡"到正文和文档.
The only way to trigger a keystroke event on document or body of a page is to trigger it in an input or textarea. In that case, the event will correctly 'bubble' to the body and document.
但是,这可能是一个问题,因为 iOS 版 Safari 不允许我们从 javascript 中提供元素焦点.
However, this might be a problem because Safari for iOS doesn't allow us to give an element focus from javascript.
目前,我们使用的解决方案是用户必须在开始第一次扫描之前点击输入字段,然后输入字段移出屏幕但保持焦点.
At the moment, we are using a solution where user has to click on an input field before starting the first scan, and the input field is then moved off-screen but retains focus.
如果有人有更好的解决方案,请分享.
If someone has a better solution, please share.
这篇关于如何添加 Javascript 侦听器以捕获从蓝牙条码扫描器到 iPad 的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!