gpt4 book ai didi

html - 我想显示我从下拉列表中选择的城市天气信息,我该如何管理它?

转载 作者:行者123 更新时间:2023-11-27 23:23:35 25 4
gpt4 key购买 nike

我想显示我从下拉列表中选择的城市天气信息,我该如何通过 json 来实现?我正在通过 Jquery 获取那个城市选择后我只想知道如何加载城市天气信息?

index.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="weather-container" style="background:#ccc;text-align:center">
<img src="" alt="" class="icon">
<p class="city"></p>
<p class="weather" style="font-size: 22px;margin:0"></p>
<p class="temp" style="font-size: 60px;margin:0;font-weight:bold"></p>
<select name="" class="select_city" id="">
<option value="Lahore">Lahore</option>
<option value="Karachi">Karachi</option>
<option value="Islamabad">Islamabad</option>
<option value="Perth">Perth</option>
<option value="Berlin">Berlin</option>
</select>
</div>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="script.js"></script>

script.js

$(document).ready(function(){
$("select.select_city").change(function(){
var selectedCountry = $(this).children("option:selected").val();
alert("You have selected the country - " + selectedCountry);
});
});
var cityy = "Lahore";
$.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + selectedCountry + "&units=metric&APPID=d89208ad904d31a4402384ff1d4d1a2f",

function(data){
console.log(data);

var icon = "https://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
$('.icon').attr("src", icon);

var temp = Math.round(data.main.temp);
$('.temp').append(temp);

var weather = data.weather[0].main;
$('.weather').append(weather);

var city = data.name;
$('.city').append(city);
});

最佳答案

您必须将“var selectedCountry”用作全局变量,以便其他函数可以使用它。范围和上下文的问题。

定义如下: var selectedCountry = null;

然后在 document.ready 中分配值而不使用 var 关键字。

selectedCountry = 通过 Javascript 选择的值

关于html - 我想显示我从下拉列表中选择的城市天气信息,我该如何管理它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57889060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com