There is functionality like, the textbox accepts input from only barcode scanner and restricts any other input from keyboard.
问问题
5421 次
2 回答
0
在http://www.deadosaurus.com/detect-a-usb-barcode-scanner-with-javascript查看此内容
从链接中,我已修改为在不符合 10 个字符长度标准时自动清除文本,这可以假设不符合 10 个字符长度标准 = 不从条形码扫描仪输入。
我正在使用 ASP.NET,下面是我的示例:
对于 ASP 代码:
<asp:TextBox ID="TextBoxComponentPartNumber" runat="server" onkeypress="AutoClearOrSetInputText(event,this.id);" ></asp:TextBox>
<asp:TextBox ID="TextBoxAssemblyPartNumber" runat="server" onkeypress="AutoClearOrSetInputText(event,this.id);" ></asp:TextBox>
对于 JavaScript:
<script type="text/javascript">
//This variables is for AutoClearOrSetInputText function
var pressed = false;
var chars = [];
//This function will auto clear or set input text box’s text value
function AutoClearOrSetInputText(eventForTextBox,idForTextBox) {
// add each entered char to the chars array
chars.push(String.fromCharCode(eventForTextBox.which));
// variable to ensure we wait to check the input we are receiving
if (pressed == false) {
// we set a timeout function that expires after 0.5 sec, once it does it clears out a list
// of characters
setTimeout(function() {
// check we have a long length e.g. it is a barcode
if (chars.length >= 10) {
// join the chars array to make a string of the barcode scanned
var barcode = chars.join(“”);
// assign value to input for barcode scanner scanned value
document.getElementById(idForTextBox).value = barcode;
}
else {
// clear value from input for non-barcode scanner scanned value
document.getElementById(idForTextBox).value = ”;
}
chars = [];
pressed = false;
}, 500);
}
// set press to true so we do not reenter the timeout function above
pressed = true;
}
</script>
于 2016-11-08T07:59:09.667 回答