gpt4 book ai didi

javascript - 将条形码扫描到特定的文本框中

转载 作者:可可西里 更新时间:2023-11-01 02:29:44 29 4
gpt4 key购买 nike

我正在研究条形码扫描仪。我使用的条形码扫描器是即插即用类型,无论您将光标放在何处,它都会自动扫描代码。但我想要的是,每次我的扫描仪读取代码时,我是否可以将它扫描到网页上的特定文本框

例如,如果我的表格看起来像这样

<input type="text" name="txtItem" id="txtItem" class="m-wrap w-120" tabindex="6">

<input type="text" name="itemId" id="itemId" class="m-wrap w-120" tabindex="6">

<input type="text" name="itemName" id="itemName" class="m-wrap w-120" tabindex="6">

<input type="text" name="itemQty" id="itemQty" class="m-wrap w-120" tabindex="6">

所以每次我扫描代码时,无论我当前的焦点在哪里,它都应该始终出现在 txtitem 文本框中。

有人可以指导我或帮助我在这里找到解决方案吗??

最佳答案

一些条形码扫描仪就像另一个输入设备一样。除非您使用计时器来监控输入信息的速度,否则表单无法区分键盘输入的信息与扫描仪输入的信息之间的区别。

一些扫描器将值“粘贴”到焦点控件中 - 其他扫描器发送每个单独的击键。

以下 JSFiddle 能够检测在单个控件上单独发送字符时何时发生输入:

http://jsfiddle.net/PhilM/Bf89R/3/

您可以对其进行调整以使其成为整个表单的委托(delegate),并从输入它的控件中删除输入并将其放入正确的表单中。

fiddle 的测试 html 是这样的:

<form>
<input id="scanInput" />
<button id="reset">Reset</button>
</form>
<br/>
<div>
<h2>Event Information</h2>
Start: <span id="startTime"></span>
<br/>First Key: <span id="firstKey"></span>
<br/>Last Ley: <span id="lastKey"></span>
<br/>End: <span id="endTime"></span>
<br/>Elapsed: <span id="totalTime"></span>
</div>
<div>
<h2>Results</h2>
<div id="resultsList"></div>
</div>

示例 fiddle 的 Javascript 是:

/*
This code will determine when a code has been either entered manually or
entered using a scanner.
It assumes that a code has finished being entered when one of the following
events occurs:
• The enter key (keycode 13) is input
• The input has a minumum length of text and loses focus
• Input stops after being entered very fast (assumed to be a scanner)
*/

var inputStart, inputStop, firstKey, lastKey, timing, userFinishedEntering;
var minChars = 3;

// handle a key value being entered by either keyboard or scanner
$("#scanInput").keypress(function (e) {
// restart the timer
if (timing) {
clearTimeout(timing);
}

// handle the key event
if (e.which == 13) {
// Enter key was entered

// don't submit the form
e.preventDefault();

// has the user finished entering manually?
if ($("#scanInput").val().length >= minChars){
userFinishedEntering = true; // incase the user pressed the enter key
inputComplete();
}
}
else {
// some other key value was entered

// could be the last character
inputStop = performance.now();
lastKey = e.which;

// don't assume it's finished just yet
userFinishedEntering = false;

// is this the first character?
if (!inputStart) {
firstKey = e.which;
inputStart = inputStop;

// watch for a loss of focus
$("body").on("blur", "#scanInput", inputBlur);
}

// start the timer again
timing = setTimeout(inputTimeoutHandler, 500);
}
});

// Assume that a loss of focus means the value has finished being entered
function inputBlur(){
clearTimeout(timing);
if ($("#scanInput").val().length >= minChars){
userFinishedEntering = true;
inputComplete();
}
};


// reset the page
$("#reset").click(function (e) {
e.preventDefault();
resetValues();
});

function resetValues() {
// clear the variables
inputStart = null;
inputStop = null;
firstKey = null;
lastKey = null;
// clear the results
inputComplete();
}

// Assume that it is from the scanner if it was entered really fast
function isScannerInput() {
return (((inputStop - inputStart) / $("#scanInput").val().length) < 15);
}

// Determine if the user is just typing slowly
function isUserFinishedEntering(){
return !isScannerInput() && userFinishedEntering;
}

function inputTimeoutHandler(){
// stop listening for a timer event
clearTimeout(timing);
// if the value is being entered manually and hasn't finished being entered
if (!isUserFinishedEntering() || $("#scanInput").val().length < 3) {
// keep waiting for input
return;
}
else{
reportValues();
}
}

// here we decide what to do now that we know a value has been completely entered
function inputComplete(){
// stop listening for the input to lose focus
$("body").off("blur", "#scanInput", inputBlur);
// report the results
reportValues();
}

function reportValues() {
// update the metrics
$("#startTime").text(inputStart == null ? "" : inputStart);
$("#firstKey").text(firstKey == null ? "" : firstKey);
$("#endTime").text(inputStop == null ? "" : inputStop);
$("#lastKey").text(lastKey == null ? "" : lastKey);
$("#totalTime").text(inputStart == null ? "" : (inputStop - inputStart) + " milliseconds");
if (!inputStart) {
// clear the results
$("#resultsList").html("");
$("#scanInput").focus().select();
} else {
// prepend another result item
var inputMethod = isScannerInput() ? "Scanner" : "Keyboard";
$("#resultsList").prepend("<div class='resultItem " + inputMethod + "'>" +
"<span>Value: " + $("#scanInput").val() + "<br/>" +
"<span>ms/char: " + ((inputStop - inputStart) / $("#scanInput").val().length) + "</span></br>" +
"<span>InputMethod: <strong>" + inputMethod + "</strong></span></br>" +
"</span></div></br>");
$("#scanInput").focus().select();
inputStart = null;
}
}

$("#scanInput").focus();

上面的代码不支持复制/粘贴,但在我们的情况下这不太可能发生。

关于javascript - 将条形码扫描到特定的文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16296342/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com