如何在javascript函数中使用输入框中的值

如何在javascript函数中使用输入框中的值,javascript,arrays,function,input,Javascript,Arrays,Function,Input,我正在尝试运行扫描以下阵列的函数: var products = [ { brand:"Healthy Boy", product:"Sweet Chilli Sauce", size: 300, measurement: "ml", barcode:909274636143, quantity:"2" }, { brand:"Kikkoman", product:"Soy Sauce", size: 1, measurement: "litre", barcode:2345324513, qu

我正在尝试运行扫描以下阵列的函数:

var products = [
{
brand:"Healthy Boy",
product:"Sweet Chilli Sauce",
size: 300,
measurement: "ml",
barcode:909274636143,
quantity:"2"
},
{
brand:"Kikkoman",
product:"Soy Sauce",
size: 1,
measurement: "litre",
barcode:2345324513,
quantity:"23"
},
{
brand:"Golden Dragon",
product:"Rice",
size: 1,
measurement: "kg",
barcode:5623593845,
quantity:"5"
}
];
用于扫描此阵列并随后显示产品信息的功能为:

 function isBarcodeValid (barcode){
  for(var i = 0; i < products.length; i++) {
     if(products [i].barcode === barcode) {
      document.querySelector('#productBrandResult').textContent = 
      (products[i].brand);
      document.querySelector('#productNameResult').textContent = 
      (products[i].product);
      document.querySelector('#productSizeResult').textContent = 
      (products[i].size);
      document.querySelector('#productMeasurementResult').textContent = 
      (products[i].measurement);
      document.querySelector('#productBarcodeResult').textContent = 
      (products[i].barcode);
      document.querySelector('#productQuantityResult').textContent = 
      (products[i].quantity);
         }
      }
      alert("invalid barcode");
      }
当我尝试以这种方式运行该函数时,我会不断收到警报(“无效条形码”)


你知道为什么吗?谢谢

您正在检查的输入值是一个字符串,但您正在将其与产品对象中的数字进行比较。试试这个:

var barcodeEntered = parseInt(document.getElementById("barcodeSearch").value);
isBarcodeValid(barcodeEntered);

document.getElementById(“barcodeSearch”).value将是一个字符串,但您的数据
barcode:909274636143
包含整数。并且您正在与将不执行类型强制的比较

试一试

(我没有超过50%的声誉只能发表评论,但是) 有一行
警报(“无效条形码”)
,如果您不需要将其显示,我会尝试将条件
if(products[I].barcode==barcode)
更改为类似
if(products[I].barcode!==barcode)
的更改

if(products [i].barcode === barcode) {

测试:

var产品=[
{
品牌:“健康男孩”,
产品:“甜辣椒酱”,
尺寸:300,
测量:“毫升”,
条形码:909274636143,
数量:“2”
},
{
品牌:“Kikkoman”,
产品:"酱油",,
尺寸:1,
计量单位:“升”,
条形码:2345324513,
数量:“23”
},
{
品牌:“金龙”,
产品:"米",,
尺寸:1,
测量:“千克”,
条形码:5623593845,
数量:“5”
}
];
函数checkLength(){
return document.getElementById(“条形码搜索”).value.length;
}
函数isBarcodeValid(条形码){
对于(变量i=0;i0&&event.keyCode===13){
var barcodeintered=document.getElementById(“barcodeSearch”).value;
isBarcodeValid(条形码输入);
}});

您可以执行
此操作,而不是多次执行
document.getElementById(“barcodeSearch”).value。其次,输入值的数据类型是字符串,如果
同一数据类型之间不存在比较。因此,使用
+

函数isBarcodeValid(条形码){
//使用筛选器获取具有所需数据类型的匹配对象数组
产品.过滤器(功能(项目){
return item.barcode===+barcode;//将字符串转换为数字
}).forEach(功能(项目){
document.querySelector(“#productBrandResult”).textContent=item.brand
})
}
document.getElementById(“条形码搜索”).addEventListener(“按键”),
功能(事件){
如果(this.value.length>0&&event.keyCode==13){
isBarcodeValid(此.value);
}
});
var乘积=[{
品牌:“健康男孩”,
产品:“甜辣椒酱”,
尺寸:300,
测量:“毫升”,
条形码:909274636143,
数量:“2”
},
{
品牌:“Kikkoman”,
产品:"酱油",,
尺寸:1,
计量单位:“升”,
条形码:2345324513,
数量:“23”
},
{
品牌:“金龙”,
产品:"米",,
尺寸:1,
测量:“千克”,
条形码:5623593845,
数量:“5”
}
];


删除
产品
[i]
之间的空格,但我不确定这是否是您需要的,或者是否正确。如果有帮助,请告诉我。:)我认为问题更多的是为什么条形码被视为无效,而不是为什么显示警报。
if (products[i].barcode === barcode.toString()) {
if(products [i].barcode === barcode) {
if(products [i].barcode == barcode) {