在 JavaScript 中检查用户输入的字符串是否在数组中
javascriptweb developmentfront end technologyobject oriented programming
我们需要编写一个 JavaScript 程序,为用户提供输入字符串值的输入。
然后,程序应根据一些硬编码的数组值检查输入值。如果输入的字符串值包含在数组中,则程序应在屏幕上打印 true,否则打印 false。
示例
其代码为 −
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>CHECK EXISTENCE</title>
</head>
<body>
<script>
const arr = ['arsenal', 'chelsea', 'everton', 'fulham',
'swansea'];
const checkExistence = () => {
const userInput = document.getElementById("input").value;
const exists = arr.includes(userInput);
document.getElementById('result').innerText = exists;
};
</script>
<input type="text" id="input">
<button onclick="checkExistence()">Check</button>
<p id='result'></p>
</body>
</html>
输出
屏幕上的输出将是 −

相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

