我正在构建一个通过php文件获取数据的网页,并使用AJAX和JQuery进行操作,将所述数据显示在表格中。
在我创建的按钮中,我希望能够将“页面”更改为不同的数据,并将它们显示在表格中。目前的问题是我通过查询解析的数据正在返回数据的主页面。
我认为问题出在我的页面数据功能中,但我不知道如何解决这个问题。
//Function to add the page data to the assigned buttons
$('#Buttons').on('click', 'button', function() {
$('#Table1 tbody').remove();
$('#Table1').append("<tbody></tbody>");
var ap = 1
for(ap; ap <= 15; ap++){
if ($(this).attr('id') == ap){
$.getJSON('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php?=retrieve&pages=' + ap, function(data){
console.log(data)
var row
var t = 0;
for(t; t < data.length; t++){
$('#Table1').append( "<tr>" + "<td>" + data[t].business + "</td>", "<td>" + data[t].address + "</td>", "<td>" + data[t].rating + "</td>", "<td>" + data[t].date + "</td>"+"</tr>")
}
})
}
}
完整的代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title> Food Ratings </title>
<style>
h1{
size:20px;
color:#F44336;
underline:bold;
}
table, th, td{
border: 1px solid #E91E63;
}
#Buttons{
padding: 15px 32px;
text-align: center;
}
p{
padding:5px;
padding-bottom:10px;
}
body{
background-color:#B3E5FC;
color:#9C27B0;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//First function to display the table
$.get('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php', {
op: "demo",
}, function(data){
var row;
console.log(data);
data = $.parseJSON(data);
for(var i = 0; i < data.length; i++){
row = $('<tr/>');
row.append( "<td>" + data[i].business + "</td>", "<td>" + data[i].address + "</td>", "<td>" + data[i].rating + "</td>", "<td>" + data[i].date + "</td>");
$('#Table1').append(row);
}
});
//Function to create the buttons to select the pages
$.get('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php', {
op : "pages",
}, function(data){
var butNum = 1;
console.log(data)
data = $.parseJSON(data);
for(butNum; butNum <= data.pages; butNum++) {
$('#Buttons').append('<button id='+butNum +'>' + butNum+ '</button>');
}
}) ;
//Function to add the page data to the assigned buttons
$('#Buttons').on('click', 'button', function() {
$('#Table1 tbody').remove();
$('#Table1').append("<tbody></tbody>");
var ap = 1
for(ap; ap <= 15; ap++){
if ($(this).attr('id') == ap){
$.getJSON('https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php?=retrieve&pages=' + ap, function(data){
console.log(data)
var row
var t = 0;
for(t; t < data.length; t++){
$('#Table1').append( "<tr>" + "<td>" + data[t].business + "</td>", "<td>" + data[t].address + "</td>", "<td>" + data[t].rating + "</td>", "<td>" + data[t].date + "</td>"+"</tr>")
}
})
}
}
});
$("#searchForm").submit(function(event) {
// Stop form from submitting normally
event.preventDefault();
// Get some values from elements on the page:
var $form = $(this),
term = $form.find( "input[name='s']").val(),
url = $form.attr("action");
// Send the data using post
var posting = $.post( 'https://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php', {s: term});
// Put the results in a div
posting.done(function(data) {
var content = $(data).find("#content");
$("#Table1").empty().append(content);
})
});
});
</script>
<body>
<h1>
Food Standards Agency
</h1>
<br>
<br>
<div id="Buttons"> </div>
<table id="Table1" cellspacing="0" width="100%">
<thead>
<tr>
<th>Business</th>
<th>Address</th>
<th>Rating</th>
<th>Date</th>
</thead>
<tbody>
</tbody>
</table>
<form action="/" id="searchForm">
<input type="text" name="s" placeholder="Search...">
<input type="submit" value="Search">
</form>
<p>
This site list all of the resturants in canterbury with their location and their rating according to the Food standards agency
</p>
</body>
</html>