JavaScript没有';在重新加载页面之前,我无法从输入字段中获取国家

JavaScript没有';在重新加载页面之前,我无法从输入字段中获取国家,javascript,html,css,Javascript,Html,Css,我正在尝试使用disease.sh API制作Corona病毒病例跟踪器,但JavaScript无法从输入字段中获取国家,除非刷新页面。我尝试删除事件侦听器并添加onclick属性,但没有成功,因此我猜测这与端点制作不正确有关 HTML: JavaScript: let case_results = document.querySelector(".case-results"); let inputField = document.getElementById('submit

我正在尝试使用disease.sh API制作Corona病毒病例跟踪器,但JavaScript无法从输入字段中获取国家,除非刷新页面。我尝试删除事件侦听器并添加onclick属性,但没有成功,因此我猜测这与端点制作不正确有关

HTML:

JavaScript:

let case_results = document.querySelector(".case-results");
let inputField = document.getElementById('submit-country');
let countrySubmitted = inputField.value;
const submit = document.getElementById("submit-button");
const URL = 'https://disease.sh/v3/covid-19/countries/';
const lastParams = '?strict=true';
const endpoint = `${URL}${countrySubmitted}${lastParams}`;


/*

Test to change text on hover, to be replaced with icons

function changeThing() {
  document.getElementById('list-item1').innerHTML = 'hduashdas';
}

function changeThingBack() {
  document.getElementById('list-item1').innerHTML = 'Home';
}


*/

//const requestURL = `https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;

//Makes a call to the API and retrieves information about a specific country
const getCases = async () => {
  try {
    const response = await fetch(endpoint, { cache: "no-cache" });
    if (response.ok) {
      const jsonResponse = await response.json();
      renderResponse(jsonResponse);
    }
  } catch (error) {
    console.log(error);
  }
};

//Clears the previous results in the div and displays new ones
function displayCases(event) {
  while(case_results.firstChild) {
    case_results.removeChild(case_results.firstChild)
  }
  getCases();
}

submit.addEventListener('click', displayCases);

const renderResponse = (res) => {
  if (res != null) {
    console.log(res.updated);
    case_results.innerHTML = `cases in ${countrySubmitted} : ${res.cases}` + '<br>';
  } 
};
let case_results=document.querySelector(“.case results”);
让inputField=document.getElementById('submit-country');
让countrySubmitted=inputField.value;
const submit=document.getElementById(“提交按钮”);
常量URL=https://disease.sh/v3/covid-19/countries/';
常量lastParams='?strict=true';
const endpoint=`${URL}${countrySubmitted}${lastParams}`;
/*
测试以更改悬停时的文本,并将其替换为图标
函数转换(){
document.getElementById('list-item1')。innerHTML='hduashdas';
}
函数changeThingBack(){
document.getElementById('list-item1')。innerHTML='Home';
}
*/
//常量请求URL=`https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;
//调用API并检索有关特定国家/地区的信息
const getCases=async()=>{
试一试{
const response=await fetch(端点,{cache:“no cache”});
if(response.ok){
const jsonResponse=await response.json();
renderResponse(jsonResponse);
}
}捕获(错误){
console.log(错误);
}
};
//清除div中以前的结果并显示新结果
功能显示案例(事件){
while(case_results.firstChild){
case_results.removeChild(case_results.firstChild)
}
getCases();
}
submit.addEventListener(“单击”,显示案例);
常数renderResponse=(res)=>{
如果(res!=null){
console.log(res.updated);
case_results.innerHTML=`cases in${countrySubmitted}:${res.cases}`+'
'; } };
我已经修改了您的javascript,并确认它可以正常工作

let case_results = document.querySelector(".case-results");
let inputField = document.getElementById('submit-country');
let countrySubmitted = inputField.value;
const submit = document.getElementById("submit-button");
const URL = 'https://disease.sh/v3/covid-19/countries/';
const lastParams = '?strict=true';
var endpoint = `${URL}${countrySubmitted}${lastParams}`;


/*

Test to change text on hover, to be replaced with icons

function changeThing() {
  document.getElementById('list-item1').innerHTML = 'hduashdas';
}

function changeThingBack() {
  document.getElementById('list-item1').innerHTML = 'Home';
}


*/

//const requestURL = `https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;

//Makes a call to the API and retrieves information about a specific country
const getCases = async () => {
  try {
    const response = await fetch(endpoint, { cache: "no-cache" });
    if (response.ok) {
      const jsonResponse = await response.json();
      renderResponse(jsonResponse);
    }
  } catch (error) {
    console.log(error);
  }
};

//Clears the previous results in the div and displays new ones
function displayCases(event) {
  countrySubmitted = inputField.value;
  endpoint = `${URL}${countrySubmitted}${lastParams}`;
  while(case_results.firstChild) {
    case_results.removeChild(case_results.firstChild)
  }
  getCases();
}

submit.addEventListener('click', displayCases);

const renderResponse = (res) => {
  if (res != null) {
    console.log(res.updated);
    case_results.innerHTML = `cases in ${countrySubmitted} : ${res.cases}` + '<br>';
  } 
};
let case_results=document.querySelector(“.case results”);
让inputField=document.getElementById('submit-country');
让countrySubmitted=inputField.value;
const submit=document.getElementById(“提交按钮”);
常量URL=https://disease.sh/v3/covid-19/countries/';
常量lastParams='?strict=true';
var endpoint=`${URL}${countrySubmitted}${lastParams}`;
/*
测试以更改悬停时的文本,并将其替换为图标
函数转换(){
document.getElementById('list-item1')。innerHTML='hduashdas';
}
函数changeThingBack(){
document.getElementById('list-item1')。innerHTML='Home';
}
*/
//常量请求URL=`https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;
//调用API并检索有关特定国家/地区的信息
const getCases=async()=>{
试一试{
const response=await fetch(端点,{cache:“no cache”});
if(response.ok){
const jsonResponse=await response.json();
renderResponse(jsonResponse);
}
}捕获(错误){
console.log(错误);
}
};
//清除div中以前的结果并显示新结果
功能显示案例(事件){
countrySubmitted=inputField.value;
端点=`${URL}${countrySubmitted}${lastParams}`;
while(case_results.firstChild){
case_results.removeChild(case_results.firstChild)
}
getCases();
}
submit.addEventListener(“单击”,显示案例);
常数renderResponse=(res)=>{
如果(res!=null){
console.log(res.updated);
case_results.innerHTML=`cases in${countrySubmitted}:${res.cases}`+'
'; } };
试试这种方法

let case_results=document.querySelector(“.case results”);
让inputField=document.getElementById('submit-country');
const submit=document.getElementById(“提交按钮”);
/*
测试以更改悬停时的文本,并将其替换为图标
函数转换(){
document.getElementById('list-item1')。innerHTML='hduashdas';
}
函数changeThingBack(){
document.getElementById('list-item1')。innerHTML='Home';
}
*/
//常量请求URL=`https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;
//调用API并检索有关特定国家/地区的信息
const getCases=async()=>{
让countrySubmitted=inputField.value;
常量URL=https://disease.sh/v3/covid-19/countries/';
常量lastParams='?strict=true';
const endpoint=`${URL}${countrySubmitted}${lastParams}`;
试一试{
const response=await fetch(端点,{cache:“no cache”});
if(response.ok){
const jsonResponse=await response.json();
renderResponse(jsonResponse);
}
}捕获(错误){
console.log(错误);
}
};
//清除div中以前的结果并显示新结果
功能显示案例(事件){
while(case_results.firstChild){
case_results.removeChild(case_results.firstChild)
}
getCases();
}
submit.addEventListener(“单击”,显示案例);
常数renderResponse=(res)=>{
如果(res!=null){
case_results.innerHTML=`cases in${res.country}:${res.cases}`+'
'; } };
。导航栏徽标{
宽度:240px;
}
img{
宽度:140px;
}
李{
利润率:20px;
字体大小:15px;
}
.个案结果{
颜色:蓝色;
}
a{
颜色:白色;
文字装饰:无;
文本转换:大写;
字体系列:Arial;
显示:内联块;
填充:16px 32px;
背景图像:线性梯度(120度,透明50%,白色50%);
背景尺寸:250%;
过渡:全部为0.4s;
字母间距:2px;
}
p、 无显示{
字体大小:16px;
颜色:白色;
保证金:0;
填充:0;
}
李:停一停{
背景位置:100%;
颜色:#313131!重要;
转化:translateX(16px);
}
@仅介质屏幕和(最大宽度:600px){
p、 无显示{
显示:无;
}
li[class=“资产净值项目”]{
文本对齐:居中;
}
}

标题
电晕病毒病例跟踪器

let case_results = document.querySelector(".case-results");
let inputField = document.getElementById('submit-country');
let countrySubmitted = inputField.value;
const submit = document.getElementById("submit-button");
const URL = 'https://disease.sh/v3/covid-19/countries/';
const lastParams = '?strict=true';
const endpoint = `${URL}${countrySubmitted}${lastParams}`;


/*

Test to change text on hover, to be replaced with icons

function changeThing() {
  document.getElementById('list-item1').innerHTML = 'hduashdas';
}

function changeThingBack() {
  document.getElementById('list-item1').innerHTML = 'Home';
}


*/

//const requestURL = `https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;

//Makes a call to the API and retrieves information about a specific country
const getCases = async () => {
  try {
    const response = await fetch(endpoint, { cache: "no-cache" });
    if (response.ok) {
      const jsonResponse = await response.json();
      renderResponse(jsonResponse);
    }
  } catch (error) {
    console.log(error);
  }
};

//Clears the previous results in the div and displays new ones
function displayCases(event) {
  while(case_results.firstChild) {
    case_results.removeChild(case_results.firstChild)
  }
  getCases();
}

submit.addEventListener('click', displayCases);

const renderResponse = (res) => {
  if (res != null) {
    console.log(res.updated);
    case_results.innerHTML = `cases in ${countrySubmitted} : ${res.cases}` + '<br>';
  } 
};
let case_results = document.querySelector(".case-results");
let inputField = document.getElementById('submit-country');
let countrySubmitted = inputField.value;
const submit = document.getElementById("submit-button");
const URL = 'https://disease.sh/v3/covid-19/countries/';
const lastParams = '?strict=true';
var endpoint = `${URL}${countrySubmitted}${lastParams}`;


/*

Test to change text on hover, to be replaced with icons

function changeThing() {
  document.getElementById('list-item1').innerHTML = 'hduashdas';
}

function changeThingBack() {
  document.getElementById('list-item1').innerHTML = 'Home';
}


*/

//const requestURL = `https://disease.sh/v3/covid-19/countries/Moldova?strict=true`;

//Makes a call to the API and retrieves information about a specific country
const getCases = async () => {
  try {
    const response = await fetch(endpoint, { cache: "no-cache" });
    if (response.ok) {
      const jsonResponse = await response.json();
      renderResponse(jsonResponse);
    }
  } catch (error) {
    console.log(error);
  }
};

//Clears the previous results in the div and displays new ones
function displayCases(event) {
  countrySubmitted = inputField.value;
  endpoint = `${URL}${countrySubmitted}${lastParams}`;
  while(case_results.firstChild) {
    case_results.removeChild(case_results.firstChild)
  }
  getCases();
}

submit.addEventListener('click', displayCases);

const renderResponse = (res) => {
  if (res != null) {
    console.log(res.updated);
    case_results.innerHTML = `cases in ${countrySubmitted} : ${res.cases}` + '<br>';
  } 
};