問題描述
我在 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 的輸入?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!