所以我有一些简单的javascript代码,我想检查用户的输入是否等于“ options”数组中的任何项目,我尝试了此方法,但是它不起作用,有人知道原因/另一种方法吗?去做这个?
var options = ["fris", "bier", "wijn"];
var bestelling_opgenomen = false;
var bestelling = prompt("Welke bestelling wilt u toevoegen?");
if(options.includes(bestelling) == bestelling){
var hoeveelheid = prompt(`hoeveel ${bestelling} wilt u bestellen?`);
}else{
alert("kurk");
}
.includes
返回一个布尔值。因此,您只是在错误地使用它。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
尝试
var options = ["fris", "bier", "wijn"];
var bestelling_opgenomen = false;
var bestelling = prompt("Welke bestelling wilt u toevoegen?");
if(options.includes(bestelling)){
var hoeveelheid = prompt(`hoeveel ${bestelling} wilt u bestellen?`);
}else{
alert("kurk");
}
您可能还想小写输入,以确保用户没有输入大写的名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句