[英]How to check if string exist in js array?
我在 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 中重要标记的更多信息)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.