【发布时间】:2019-08-12 23:10:25
【问题描述】:
我想我在这里遗漏了一些东西。 此 Jquery Mobile Web 应用程序用于查看特定城市的停车场信息。
基本上,应用程序正在加载、解析 JSON 数据,然后将其附加到页面上。然后是一个文本框输入和一个按钮。基于文本框输入,它只显示与文本框输入中指定的相同数量的停车位的数据。基本上是根据用户指定的停车位数量查看停车场信息。 我让它有点工作,但附加数据的方法在按钮单击时不起作用。只有当我在本地浏览器上刷新页面时,它才会加载与文本框对应的值。我不希望这样,并且希望在单击按钮时更新列表。
JSONparkingLots.JSON 的一部分
{
"lotList":[
{
"id":1,
"lotName":"Jarvis St \/ Carleton St",
"availableSpots":8,
"totalSpots":15
}
]
}
main.js
var lotData;
$(document).ready(function()
{
$.getJSON({
url: "parkingLots.json",
mimeType: "application/json",
success: parseParkingLotsJSON
});
});
function parseParkingLotsJSON(data)
{
lotData = data.lotList;
for (var i=0; i < lotData.length; i++)
{
$("#size").append(appendLot(lotData[i], i));
};
}
function appendLot(lotData)
{
var lotName = lotData.lotName;
var lotId = lotData.id;
var lotAmount = lotData.availableSpots;
var key = $("#sizeId").val();
var size = $("<div " + lotId + "' data-role='table'>");
if ( lotAmount == key ) {
size.append("<h1>" + lotName + "</h1>");
size.append("<p> Lot ID: " + lotId + "</p>");
size.append("<p> Spots: " + lotAmount + "</p>");
}
return size;
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Parking Lot Web</title>
<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="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="main.css" />
<script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.31"></script>
<script src="main.js"></script>
</head>
<h1>Parking Lot App</h1>
<body class="ui-page-theme-a">
<div data-role="page" id="home">
<h1 class="center">Parking App</h1>
<ul>
<a data-role="button" href='#page1'>Group Size</a>
</ul>
</div>
<div id="page1" data-role="page" data-filter="true" class="center">
<!--Where it's displayed on page-->
<h1 class="center" data-role="content">Search by Lot Size</h1>
<input type = "text" id = "sizeId"/>
<input type = "button" id = "sizeBtn" value = "search" onclick="appendLot(lotData)"/>
<div id="size" data-role="table" class="center"></div>
</div>
</div>
</body>
</html>
如果您有任何建议或有办法让我能够在单击按钮时加载数据而不必刷新,我将不胜感激。
【问题讨论】:
-
此应用程序是否尝试加载数据,将其绑定到页面,然后根据可用插槽上的匹配搜索批次?
-
是的,它应该只显示与用户指定的停车位数量相同的地段的数据。 prntscr.com/n191sf 这就是我想要它做的事情。它显示有 8 个可用停车位的停车场信息,如文本框中指定的那样。唯一的问题是我只能通过刷新此页面来获得此结果。当我单击按钮时,列表不会更新,只能通过刷新来更新。我想让它在点击搜索按钮时工作。
标签: javascript jquery html json jquery-mobile