Доброго времени суток.
Появилась проблема с переменными. А именно, есть 2 файла, в одном я ввожу город по которому хочу получить инфу и хочу его значение получить во втором файле, чтобы вывести по нему прогноз. В первом файле это переменная city, мой нерабочий вариант для переменной во втором файле я привёл ниже(forecast_city).
HTML5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| <!DOCTYPE html>
<HTML>
<HEAD>
<meta charset="utf-8" />
<!-- The stylesheet -->
<link rel="stylesheet" href="assets/css/styles.css" />
<TITLE>Прогноз погоды</TITLE>
<SCRIPT type="text/javascript">
<!--
function validate_form ( )
{
valid = true;
if ( document.forecast_form.city.value == "" )
{
alert ( "Пожалуйста, заполните поле 'Город'." );
valid = false;
}
if ( document.forecast_form.datetime.value == "" )
{
alert ( "Пожалуйста, заполните поле 'Дата'." );
valid = false;
}
return valid;
}
function clean()
{
document.forecast_form.city.value="";
}
function clean()
{
document.forecast_form.datetime.value="";
}
</SCRIPT>
</HEAD>
<body>
<FORM name="forecast_form" method="post" action="index.html" onsubmit="return validate_form();">
<header>
<h1>Заполните поля</h1>
</header>
<div id="weather">
<a> Город: <INPUT type="text" name="city" ></a>
<a> Дата: <INPUT type="datetime" name="date" ></a>
<br>
<p><INPUT type="submit" name="send" value="Узнать" onclick="document.getElementById('index.html').submit(); clean()"></p>
</div>
</FORM>
</BODY></HTML> |
|
HTML5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
| <!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
type="text/javascript"></script>
<link rel="stylesheet"
href="http://openweathermap.org/themes/openweathermap/assets/css/styles.306e615736ca30911eba.css" />
<style>
.weatherMainClass {
width: 50%;
overflow-y: scroll;
height: 85%;
margin-left: auto;
margin-right: auto;
}
.center_align {
text-align: center;
}
</style>
<script>
var valid_appid = "019a736fd448ec0464f324f3f7063003";
var invalid_appid = "sridhar";
var reference_URL = "http://api.openweathermap.org/data/2.5/forecast?APPID="
+ valid_appid + "&units=metric&q=Kharkiv,IN&mode=json";
$(document).ready(function() {
$.ajax({
type : "GET",
url : reference_URL,
dataType : "json",
success : jsonParser,
error : errorMethod
});
});
function jsonParser(json) {
console.log(json);
var open_tr = '<tr class="daily-list__today">';
var open_td = '<td>';
var close_td = '</td>';
var close_tr = '</tr>';
var final_div_content = "";
var i = 0;
var city;
$(json.list)
.each(
function(idx, obj) {
var weather_date = obj.dt_txt;
var temp_max = obj.main.temp_max;
var temp_min = obj.main.temp_min;
var wind_speed = obj.wind.speed;
var weather_clouds = obj.clouds.all;
var weather_grnd_level = obj.main.grnd_level
var weather_icon;
var weather_description;
var today_div = "";
$(obj.weather)
.each(
function(idx, weatherObj) {
weather_icon = '<img src="http://openweathermap.org/img/w/'+weatherObj.icon+'.png" />';
weather_description = weatherObj.description;
});
if (i == 0)
today_div = '<div class="daily-list__today-label">Сегодня</div>';
final_div_content += open_tr;
final_div_content += open_td + weather_date
+ weather_icon + today_div + close_td;
final_div_content += open_td
+ '<span class="label label-warning">'
+ temp_max + '℃</span> ';
final_div_content += '<span class="label label-default">'
+ temp_min
+ '℃</span> <i>'
+ weather_description + '</i>';
final_div_content += '<p>' + wind_speed
+ ' m/s</br>clouds: ' + weather_clouds
+ ' %, ' + weather_grnd_level + ' hpa</p>'
final_div_content += close_td;
final_div_content += close_tr;
//console.log("final_div_content"+final_div_content);
i++;
});
$(json.city).each(function(idx, obj) {
city = obj.name;
});
$(".weather-graphic__header")
.html("Прогноз на 5 дней для " + city);
$("#weatherTable").html(final_div_content);
}
function errorMethod() {
alert("Ошибка");
$('.weatherMainClass').hide();
$('#error_block').html(
'Ошибка');
}
</script>
<meta charset="utf-8">
<title>Прогноз погоды</title>
</head>
<body>
<div class="center_align">
<h3 class="weather-graphic__header"></h3>
</div>
<div id="error_block" class="center_align"></div>
<div class="weatherMainClass" id="">
<table id="weatherTable" class="table daily-list">
</table>
</div>
</body>
</html> |
|
Вот были мои потуги, но так не работает
Javascript |
1
2
3
4
5
6
| <script>
var valid_appid = "019a736fd448ec0464f324f3f7063003";
var invalid_appid = "sridhar";
var forecast_city == forecast_form.city;
var reference_URL = "http://api.openweathermap.org/data/2.5/forecast?APPID="
+ valid_appid + "&units=metric" + "&q=" + forecast_city + ",IN&mode=json"; |
|