JS中如何获取select的值

发布于 2025-01-13 15:49:33 字数 1876 浏览 0 评论 0原文

如何获取选择更改城镇的值以获得天气信息? 我尝试添加一些事件侦听器,但似乎无法使其正常工作。 我到底需要在哪里添加函数或事件侦听器才能获取值?抱歉,我试图理解这一切,但我仍然不确定。

const key = '';
if (key == '') document.getElementById('temp').innerHTML = ('');

function weatherApp(name) {
  fetch('https://api.openweathermap.org/data/2.5/weather?q=' + name + '&appid=' + key)
    .then(function(resp) {
      return resp.json()
    }) // Convert data to json
    .then(function(data) {
      drawWeather(data);
    })
    .catch(function() {
      // catch any errors
    });
}

function drawWeather(d) {
  var celcius = Math.round(parseFloat(d.main.temp) - 273.15);
  var fahrenheit = Math.round(((parseFloat(d.main.temp) - 273.15) * 1.8) + 32);
  var description = d.weather[0].description;

  document.getElementById('description').innerHTML = description;
  document.getElementById('temp').innerHTML = celcius + '°';
  document.getElementById('location').innerHTML = d.name;

  if (description.indexOf('rain') > 0) {
    document.body.className = 'rainy';
  } else if (description.indexOf('cloud') > 0) {
    document.body.className = 'cloudy';
  } else if (description.indexOf('sunny') > 0) {
    document.body.className = 'sunny';
  } else {
    document.body.className = 'clear';
  }
}

window.onload = function() {
  weatherApp("Manchester");
}
<select class="form-control" id="selectCountry">
  <option>Select a country</option>
  <option disabled="">_________</option>
  <option value="Kabul,AF">Afghanistan</option>
  <option value="Luanda,AO">Angola</option>
  <option value="Canberra,AU">Australia</option>
  <option value="Vienna,AT">Austria</option>
</select>

How can I get the value on select to change the town in order to get the weather info?
I tried adding some event listeners but cannot seem to get it working.
Where exactly do I need to add the function or the event listener to get the value? Sorry, I am trying to understand it all but I'm still not sure.

const key = '';
if (key == '') document.getElementById('temp').innerHTML = ('');

function weatherApp(name) {
  fetch('https://api.openweathermap.org/data/2.5/weather?q=' + name + '&appid=' + key)
    .then(function(resp) {
      return resp.json()
    }) // Convert data to json
    .then(function(data) {
      drawWeather(data);
    })
    .catch(function() {
      // catch any errors
    });
}

function drawWeather(d) {
  var celcius = Math.round(parseFloat(d.main.temp) - 273.15);
  var fahrenheit = Math.round(((parseFloat(d.main.temp) - 273.15) * 1.8) + 32);
  var description = d.weather[0].description;

  document.getElementById('description').innerHTML = description;
  document.getElementById('temp').innerHTML = celcius + '°';
  document.getElementById('location').innerHTML = d.name;

  if (description.indexOf('rain') > 0) {
    document.body.className = 'rainy';
  } else if (description.indexOf('cloud') > 0) {
    document.body.className = 'cloudy';
  } else if (description.indexOf('sunny') > 0) {
    document.body.className = 'sunny';
  } else {
    document.body.className = 'clear';
  }
}

window.onload = function() {
  weatherApp("Manchester");
}
<select class="form-control" id="selectCountry">
  <option>Select a country</option>
  <option disabled="">_________</option>
  <option value="Kabul,AF">Afghanistan</option>
  <option value="Luanda,AO">Angola</option>
  <option value="Canberra,AU">Australia</option>
  <option value="Vienna,AT">Austria</option>
</select>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2025-01-20 15:49:33

根据我的理解,当 select 元素上的值发生更改时,您希望调用 weatherApp 函数。

为此,请使用 onchange< select 的 /a> 事件 元素。
用于选择 元素有改变了。

const key = '';

function weatherApp(name) {
  fetch('https://api.openweathermap.org/data/2.5/weather?q=' + name + '&appid=' + key)
    .then(function(resp) {
      return resp.json()
    }) // Convert data to json
    .then(function(data) {
      drawWeather(data);
    })
    .catch(function() {
      // catch any errors
    });
}

const formControl = document.getElementsByClassName("form-control")[0];

formControl.addEventListener("change", function() {
  weatherApp(formControl.value);
})

function drawWeather(d) {
  var celcius = Math.round(parseFloat(d.main.temp) - 273.15);
  var fahrenheit = Math.round(((parseFloat(d.main.temp) - 273.15) * 1.8) + 32);
  var description = d.weather[0].description;

  document.getElementById('description').innerHTML = description;
  document.getElementById('temp').innerHTML = celcius + '°';
  document.getElementById('location').innerHTML = d.name;

  if (description.indexOf('rain') > 0) {
    document.body.className = 'rainy';
  } else if (description.indexOf('cloud') > 0) {
    document.body.className = 'cloudy';
  } else if (description.indexOf('sunny') > 0) {
    document.body.className = 'sunny';
  } else {
    document.body.className = 'clear';
  }
}

window.onload = function() {
  weatherApp("Manchester");
}
<select class="form-control" id="selectCountry">
  <option>Select a country</option>
  <option disabled="">_________</option>
  <option value="Kabul,AF">Afghanistan</option>
  <option value="Luanda,AO">Angola</option>
  <option value="Canberra,AU">Australia</option>
  <option value="Vienna,AT">Austria</option>
</select>

Based on what I understood, you want to call the weatherApp function when the value on the select element has changed.

For this, use the onchange event for the select element.
This event will fire when the value for a select element has changed.

const key = '';

function weatherApp(name) {
  fetch('https://api.openweathermap.org/data/2.5/weather?q=' + name + '&appid=' + key)
    .then(function(resp) {
      return resp.json()
    }) // Convert data to json
    .then(function(data) {
      drawWeather(data);
    })
    .catch(function() {
      // catch any errors
    });
}

const formControl = document.getElementsByClassName("form-control")[0];

formControl.addEventListener("change", function() {
  weatherApp(formControl.value);
})

function drawWeather(d) {
  var celcius = Math.round(parseFloat(d.main.temp) - 273.15);
  var fahrenheit = Math.round(((parseFloat(d.main.temp) - 273.15) * 1.8) + 32);
  var description = d.weather[0].description;

  document.getElementById('description').innerHTML = description;
  document.getElementById('temp').innerHTML = celcius + '°';
  document.getElementById('location').innerHTML = d.name;

  if (description.indexOf('rain') > 0) {
    document.body.className = 'rainy';
  } else if (description.indexOf('cloud') > 0) {
    document.body.className = 'cloudy';
  } else if (description.indexOf('sunny') > 0) {
    document.body.className = 'sunny';
  } else {
    document.body.className = 'clear';
  }
}

window.onload = function() {
  weatherApp("Manchester");
}
<select class="form-control" id="selectCountry">
  <option>Select a country</option>
  <option disabled="">_________</option>
  <option value="Kabul,AF">Afghanistan</option>
  <option value="Luanda,AO">Angola</option>
  <option value="Canberra,AU">Australia</option>
  <option value="Vienna,AT">Austria</option>
</select>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文