Ajax下拉列表,用于搜索输入以使用雅虎天气查找天气


Ajax dropdown list to search input to find weather using Yahoo Weather

我有这个代码,如果选择,它将给出一个城市的下拉列表,将为我提供该特定城市的天气。数据是使用 Yahoo API 提取的。

这是代码,

<HTML>
<HEAD>
<TITLE>Find Weather in major cities around the worl</TITLE>
<META NAME="Keywords" CONTENT="ajax, weather, api, fetch, yahoo">
<META NAME="Description" CONTENT="yahoo, weather">
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<SCRIPT type="text/javascript">
<!--
$(document).ready(function(){
function get_weather()
{
var p = $("#code").val();
var u = ($('#u').attr('checked')) ? '&u=c' : '';
var to_load = 'get_weather.php?p='+ p + u;
$("#weather").html('<img style="margin-top: 104px;" src="ajax-loader.gif"   align="absmiddle">');
$("#weather").load(to_load);
}
$(window).load(get_weather); // Trigger "get_weather" when the window loads
// Trigger "get_weather" when the form objects are used
$("#code").change(get_weather);
$("#u").click(get_weather); 
});
//-->
</SCRIPT>
</HEAD>
<BODY>
<A target="_blank" HREF=""></A>
<center>
<div align="center">
<h2 align="center">Find World Weather</h2>
<form>
Get Weather for: <select id="code" name="code">
<option value="AFXX0001">Ghurian</option>
<option value="AFXX0002">Herat</option>
<option value="UKXX0687">Buckingham, United Kingdom</option>
<option value="ASXX0001">Adelaide, Australia</option>
</select>
<input type="checkbox" id="u" name="u" value="c">&nbsp;Change to degrees Celsius
</form>
<div id="weather"></div>
</div>
</center>
</BODY>
</HTML>

但是我需要的是下拉列表位置的搜索框,当我进入一个城市时,我应该得到那个地方的天气信息。我是初学者,所以请这样对待我,不要复杂化你的所有答案。提前非常感谢。

我认为您可能会为此而苦恼,因为您需要了解雅虎对每个位置的 WOEID。 从外观上看,没有简单的方法可以以编程方式获得它。 在我看来,您最好使用其他气象服务,即http://api.openweathermap.org/data/2.5/weather?q=Birmingham,uk

他们似乎自己对地址进行一些地理编码,但您始终可以将谷歌地理编码 API 与此服务结合使用。