javascript - 无法让 .getJSON 触发 onclick
问题描述
我正在尝试让“restapp”功能触发 onclick,但它似乎不起作用。我不确定问题是什么。有什么建议么?
这是我的代码:
<html>
<head>
<title>Restaurant App</title>
</head>
<body>
<input id="info" type="button" onclick="restapp()" value="Create Table From JSON">
<hr>
<pre id="demo"></pre>
<script>
result = [];
city = 'chicago';
opURL = 'http://opentable.herokuapp.com/api/restaurants?city=' + city;
function restapp() {
$.getJSON(opURL, function(data) {
//data is the JSON string
for (i=0; i<data.restaurants.length; i++) {
result.push("Name: " +
data.restaurants[i].name);
result.push("Address: " +
data.restaurants[i].address);
result.push("Price: " +
data.restaurants[i].price);
result.push("<br>");
}
document.getElementById("demo").innerHTML = JSON.stringify(result, null, 2);
});
}
</script>
</body>
</html>
解决方案
你在哪里JQuery
?
$.getJSON 需要 JQuery。检查您的浏览器控制台是否有错误。
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
将此插入您的head
标签中。
您可以使用 JQuery 选择器来选择您的输入字段并使用 click 方法进行点击事件。
推荐阅读
- qt - 关于 Qt、UWP、OpenGL (ES) 和 ANGLE 的问题
- node.js - Express POST Request SyntaxError: 位置 1 处 JSON 中的意外标记 o
- javascript - 如何将excel列表导出为html列表
- xamarin - MyListView 不会更新 Xamarin 表单
- python - 如何解决使用 toc 将 Jupyter Notebook 下载为 HTML 时出现的错误?
- python - PyOWM“观察”对象没有属性“观察”
- python-poetry - Python工具Poetry如何知道项目虚拟环境的路径?
- c# - 如何从 ASP.NET Core 中的 HTTPS 重定向中排除路径?
- python - 多重继承:缺少实际不需要的必需位置参数
- python - 获取按钮内部的数据。BS4 + 请求