该代码旨在将一些手动输入的假日目的地显示为无序列表,然后允许用户从下拉列表中进行选择,单击“提交”并获取当前天气信息。我的问题;
谢谢!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>TRIPIO</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body onload="getDate()">
<div class="application">
<h2>TRIPIO</h2>
<p id="user"></p>
<hr/>
<h3>Top Vacation Deals <span id="date"></span></h3>
<ul id="travel"></ul>
<h4>Current weather info about a destination</h4>
<form id="destform">
<select id="list" class="field"></select><br/><br/>
<input type="submit" value="Submit" class="submit">
</form>
<div id="information"></div>
</div>
<script>
var loginname = sessionStorage.getItem('username');
document.getElementById('user').innerHTML = 'Logged in as '+ loginname;
function getDate() {
var d = new Date();
document.getElementById('date').innerHTML = d;
}
var holidays = [
{
destination: 'Dubai',
price: 350000
},
{
destination: 'Paris',
price: 470000
},
{
destination: 'Mauritius',
price: 250000
},
{
destination: 'South Africa',
price: 300000
}
];
for(var i = 0; i < holidays.length; i++){
console.log(holidays[i].price+' is the price for a trip to '+holidays[i].destination);
document.getElementById('travel').innerHTML += '<li>'+ holidays[i].price + ' is the price for a trip to ' + holidays[i].destination+'</li>';
document.getElementById('list').innerHTML += '<option>' +holidays[i].destination + '</option>';
}
function getInfo(e){
e.preventDefault();
var x = document.getElementById('list');
var selection = x.options[x.selectedIndex].text;
console.log(selection);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200){
var weather = JSON.parse(xhttp.responseText);
document.getElementById("information").innerHTML = "<h5>The weather is ${weather.temp} degrees farenheit</h5>";
}
}
xhttp.open('GET', 'api.openweathermap.org/data/2.5/weather?q={'+selection+'}&APPID={602fb577e945c38c4df26c7504bf23ff}',true);
xhttp.send();
}
document.getElementById('destform').addEventListener('submit', getInfo, false);
</script>
<footer>
<p>This web application is running purely on Javascript ©</p>
</footer>
</body>
</html>