jquery - 如何使用 jQuery 根据用户输入过滤 API 值
问题描述
如何根据用户输入字段将城市(q)等于伦敦更改为纽约?
场景:用户输入城市名称,根据城市名称显示温度。
$.getJSON('https://api.openweathermap.org/data/2.5/weather?q=London&units=metric&appid={key}', function(data){
});
我尝试了下面的代码。但无法得到预期的结果。
$(function(){
$(".submit_btn").on('click', function(){
var city = $("#city_name").val();
$.getJSON('https://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&appid={key}', function(data){
var temp = `Temperature: ${data.main.temp} degrees Celsius`
$(".w1").text(temp);
});
});
});
解决方案
最后,它的工作方式如下;
$.getJSON(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid={key}`, function(info){
var temp = `Temperature: ${info.main.temp} degrees Celsius`
$(".w1").text(temp);
});
解决方案:在 URL 的任一侧添加反引号 (`)。
推荐阅读
- kubernetes - 使用 helm install 传递配置、证书和机密的最佳实践
- c# - 如何使用 api c# 从简单的 redmine 中获取所有未解决的问题
- bash - ':在 bash 中不是有效的标识符释放错误消息
- ios - 使用 FCM 时生成证书的正确方法是什么
- r - 如何在 dplyr 中使用 substr 在 R 中选择
- python - AWS API 网关 Lambda 授权程序警告
- google-chrome-extension - 如何从 chrome 网上商店下载原始源代码
- c++ - 带 4K 显示的 QT 布局输出
- svelte - 无法动态加载组件
- kubernetes - 通过队列使用 kubernetes 动态创建 Pod