我们有一个用于扫描库存的网页。我们使用自己的自定义条形码来检查和进出。这些是按顺序排列的,例如0000024,0000025,0000026等。这些条形码中的每一个都具有与其一起的项目的相应描述。我想知道是否有一种方法,当将条形码扫描到网页时,它可以自动替换条形码编号和项目描述。我正在玩字符串替换,但我不确定如何为项目列表执行此操作。
答案 0 :(得分:1)
您可以创建一个将条形码映射到其描述的对象文字:
var barCodeMap = {
"0000024": "description 24",
"0000025": "description 25",
"0000026": "description 26"
},
scannedBarCode = "0000025",
descriptionOfScannedBarCode = barCodeMap[scannedBarCode];
console.clear();
console.log(descriptionOfScannedBarCode);
(网站可以使用JSON format从AJAX中的服务器下载地图。可以通过调用JSON.parse()将下载的JSON字符串转换为JavaScript对象。)
答案 1 :(得分:0)
您可以创建一个对象,然后在其中搜索条形码。
var barcodes = {
"000555":"This is a discription for 000555",
"000666":"This is a discription for 000666"
}
然后当输入值的变化查找它然后将它放在p元素中时。
$("input[type=text]").on("input", function(){ //on value changed
this.value //text in the textbox
$("p").text(barcodes[this.value]); //find the value of the text box in the barcodes object
});