Я написал код для поиска текущей погоды с помощью Open Weather Map. Я могу заставить код работать, когда форма и кнопка находятся в теле. Но когда я копирую и вставляю этот код в область панели навигации, он не работает. Я на 100% уверен, что это ошибка разработчика, но я хотел узнать, может ли кто-нибудь указать, где я ошибаюсь.
Чтобы упростить эту задачу, я добавил ссылку на свой код: https://codepen.io/rob-connolly/pen/wvawLOy
$(document).ready(function() {
$('#submitWeather').click(function() {
var city = $('#city').val();
if (city != '') {
$.ajax({
url: 'https://api.openweathermap.org/data/2.5/forecast?appid=34fd31758b449ea433e05058c225793c&q=' + city + "&units=imperial&count=10",
type: "GET",
dataType: "jsonp",
success: function(data) {
var widget = show(data);
$("#show").html(widget);
$("#city").val('');
}
});
} else {
$("#error").html('Field cannot be empty');
}
});
});
function show(data) {
return data.city.name + ' (' + data.list[0].dt_txt.split(' ')[0] + ') </h3>' +
'<p><strong>Temp: </strong>' + data.list[0].main.temp + ' degrees</p>' +
'<p><strong>Humidity: </strong>' + data.list[0].main.humidity + ' %</p>' +
'<p><strong>Wind Speed: </strong>' + data.list[0].wind.speed + ' MPH</p>';
}
console.log('hello')
.mainArea {
background-color: lightgray;
}
.day1,
.day2,
.day3,
.day4,
.day5 {
width: 220px;
height: 200px;
background-color: blue;
position: relative;
color: white;
}
.input {
text-align: center;
}
input[type='text'] {
height: 50px;
width: 200px;
background: #e7e7e7;
}
input[type='submit'] {
height: 50px;
width: 100px;
background: blue;
}
<!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">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Weather Dashboard</title>
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- this is the form for the weather search-->
<form class="weatherSearch form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" id="city" type="search" placeholder="Search City" aria-label="Search">
<button id="submitWeather" class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class='col-8'>
<!-- Thi is the div id = show to display the weather data. -->
<div id="show">
</div>
</div>
</div>
<div class='container'>
<div class='row'>
<div class='col-2 day1' id="show1">
hello</div>
<div class='col-2 day2'>
hello </div>
<div class='col-2 day3'>
Hello </div>
<div class='col-2 day4'>
Hellp </div>
<div class='col-2 day5'>
Hellp </div>
</div>
</div>
</div>
<!-- Main Content -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>