AjaxjQuery版
作者:互联网
<head>
<meta charset="utf-8">
<title>实时天气</title>
<script src="jquery-3.5.1.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<select name="citys">
<option value="101110101">西安</option>
<option value="101100101">太原</option>
<option value="101180101">郑州</option>
<option value="101270101">成都</option>
<option value="101160101">兰州</option>
<option value="101200101">武汉</option>
<option value="101040100">重庆</option>
<option value="101170101">银川</option>
</select>
<div id="tianqidiv"></div>
<script>
//var $cityidObj = $("#citys");
var $cityidObj = $("select[name='citys']");
var cityid =$cityidObj.val();
var cityname = $cityidObj.children().first().text();
//var xiAn = cityname;
window.onload = function(){
getTianqi()
}
$cityidObj.on("change",function(){
getTianqi($(this).val());
});
function getTianqi(csid){
if(csid){
cityid = csid;
}
$cityidObj.children().each(function(index,option){
if(cityid == $(option).val()){
cityname = $(option).text();
}
});
var tiqiUrl = "https://devapi.qweather.com/v7/weather/now?"+
"location="+cityid+"&key=????????????????";
$("#tianqidiv").load(tiqiUrl,function(responseTXT,statusTXT,xhr){
if(statusTXT == "success"){
tiqiObj = JSON.parse(responseTXT);
$(this).html(cityname+"实时天气</br>"+"天气:"+tiqiObj.now.text+"</br>温度:"+tiqiObj.now.temp);
}
if(statusTXT == "error"){
$(this).html("出错编码:"+xhr.status+"</br>出错信息"+statusTXT);
}
})
}
</script>
</body>
标签:function,statusTXT,cityname,cityidObj,AjaxjQuery,cityid,var 来源: https://blog.csdn.net/m0_51579165/article/details/111032539