我在js中有一个数组,希望用户检查其产品编号是否存在于数组中。代码是:
var arr = ['3bktzgo4',
'2ltold4k',
'l4u93l4r'];
function check() {
var input = document.getElementById("Input").value;
for (i = 0; i < arr.length; i++) {
if (arr[i] == input) {
alert("congratulations! Product code found! Your product is original!");
return;
}
}
alert("Product code not found! Your product is counterfeit!");
};
<div>
<button onclick="beginhere()">Search</button>
<input id='Input'>
</div>
<html !DOCTYPE html>
<head>
</head>
<body>
<script>
var arr = ['3bktzgo4',
'2ltold4k',
'l4u93l4r'];
function check() {
var productNumber = document.getElementById("productNumberId").value;
console.log(productNumber);
if(arr.indexOf(productNumber) != -1)
return alert("congratulations! Product code found! Your product is original!");
return alert("Product code not found! Your product is counterfeit!");
};
</script>
<div>
<button onclick="check()">Search</button>
<input id='productNumberId'>
</div>
</body>
</html>
我已经创建了此代码段,希望您知道您需要在代码中进行哪些错误或更改。
<script>
标记内(了解有关HTML中重要的tags的更多信息。