当我输入France
or 时france
,数组中的一个国家,在输入字段中,代码块返回country not in our database
。我希望代码返回France is a country located in Europe
。
我试过return
在每个条件语句上使用关键字,但没有成功。我还阅读了w3schools.com 关于if
/ else
statement的文档。
但是,我仍然无法解决这个问题。有趣的是,该else if
语句有效。我的意思是当我将输入字段留空然后单击按钮时,代码确实返回field cannot be left blank
。我知道我的问题可能听起来很基本,但我仍然是初学者。
const btn = document.getElementById("button");
btn.addEventListener("click", function(){
fetch("https://restcountries.eu/rest/v2/all")
.then(function(response){
return response.json()
})
.then(function(data){
var userInput = document.getElementById("userInput");
var userInputValue = userInput.value;
var region = document.getElementById("region");
for(var i = 0; i < data.length; i++) {
if(userInputValue.toLowerCase() === data[i].name.toLowerCase()) {
region.innerHTML = `${data[i].name} …
Run Code Online (Sandbox Code Playgroud)