javascript - 我是否为这个天气应用程序正确设置了 API?

标签 javascript html json

我正在使用天气应用程序 API,并尝试根据用户在表单中插入的内容显示天气数据。由于某种原因,数据未显示。我已经把代码检查了10遍了,但我似乎找不到错误。

有人可以看看我哪里出了问题吗?

谢谢

$(document).ready(function() {
  $('submitWeather').click(function() {
    var city = $('#city').val();

    if (city != '') {

      $.ajax({

        url: 'https://api.openweathermap.org/data/2.5/weather?q=' + city + "&units=metric" + "&APPID=_MY_API_KEY_",
        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 '<h3><strong>Weather<strong>: ' + data.weather[0].main + ' </h3>' +
    '<h3><strong>Weather<strong>: ' + data.weather[0].description + ' </h3>';
}

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-light bg-light jumbotron">
    <div class="navbar-brand">Weather Guide</a>
      <!-- <form class="form-inline">
            <input class="inputValue form-control mr-sm-2" type="text" placeholder="Search City" aria-label="Search">
            <button class="button btn btn-outline-success my-2 my-sm-0" value="submit" type="submit">Search</button>
        </form>-->
  </nav>

  <div class="container">
    <div class="row">
      <h3 class="text-center text-primary">Enter City Name</h3>
    </div>
    <div class='row form-group form-inline'>
      <input tyepe='text' name='city' id="city" class="form-control" placeholder="city name">
      <button id="submitWeather" class="btn btn-primary">Search</button>
    </div>

    <div id="show">

    </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>

最佳答案

$('submitWeather') 应该是 $('#submitWeather')

关于javascript - 我是否为这个天气应用程序正确设置了 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60048778/

相关文章:

javascript - 新滚动器 - 使用 javascript 替换 div 后不滚动

html - CSS、DIV 和 H1

asp.net - JSON:序列化从 IEnumerable 派生的类型

javascript - 将 MVC ViewModel 转换为外部 .js 文件中的 Javascript 对象

javascript - 在没有 Javascript 的情况下使用依赖字段

html - 我如何使用 flex 容器 css 属性创建此布局

javascript - 解码保存在数据库列中的 JSON 数据

javascript - JSON 初学者 - 何时使用 JSON?

javascript - 身份验证的架构和思考

JavaScript 习语 : ! something && function()