jquery - 我的表单和搜索与我的城市天气搜索的 jquery 代码交互的问题
问题描述
我已经编写了代码来使用 Open Weather Map 搜索今天的天气。当表单和按钮在正文中时,我能够使代码工作。但是当我将该代码复制并粘贴到导航栏区域时,它不起作用。我 100% 确定这是开发人员的错误,但我想看看是否有人能指出我哪里出错了。
为了使这更容易,我提供了一个指向我的 codepen 的链接: 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>
解决方案
您必须阻止表单提交。这是您的笔正常工作https://codepen.io/bigbadmytyx/pen/poJzMjY
我添加了这个:
$("#submitWeather").click(function(e) {
e.preventDefault();
它可以防止表单合并,而是触发 ajax 请求。
推荐阅读
- database - 通过model_type和model_id多态多对多获取关系并对结果数据进行分页
- reactjs - 在数字海洋的反应应用程序中上传图像
- virtual-machine - 虚拟机,通过网络 USB 集线器模拟 USB
- interop - 请求票证系统是否有任何开放的导入/导出格式、DSL 或其他互操作性工具?
- flutter - 如何仅从一侧 FLUTTER 使上滑面板城市成为圆形
- python - 基于组内的对进行过滤
- sql - Redshift SQL Rolling SUM OVER last 3 ids by timestamp(每个 id 有多行)
- python - 以编程方式在 CircuitPython 中创建 digitalio.DigitalInOut 对象
- r - 野生集群引导三明治和 lmtest 中的小样本校正
- amazon-web-services - 创建用于拍摄 EBS 快照的 EventBridge 事件规则的更快方法