3 回答

TA贡献1812条经验 获得超5个赞
一种可能的解决方案是for loop使用Array.includes()替换整个示例,例如:
let password = new Array("pass1", "pass2", "pass3", "pass4", "pass5");
let button = document.getElementById("button");
let input = document.getElementById("input1");
button.addEventListener("click", function()
{
if (password.includes(input.value))
alert("welcome");
else
alert("wrong");
});
<input type="text" id="input1">
<button type="button" id="button">Button</button>

TA贡献1834条经验 获得超8个赞
您需要检查数组的元素,并在欢迎之后返回是否找到该值。
在循环后将错误的警报置于末尾,因为对于每个不匹配的密码,您都会收到更多警报。
button.addEventListener("click", function () {
for (var x = 0; x <= password.length; x++) {
if (input.value == password[x]) {
alert("welcome"); // or take document.getElementById('someid').innerHTML = 'welcome!'
return;
}
}
alert("wrong");
});
添加回答
举报