我目前正在尝试使用AJAX从PHP文件中加载一些JSON图像数据,并使用jQuery通过单击下一个/上一个按钮循环结果。当我指示指定的索引(data.record[0].image;)时,我可以成功加载数据但我希望它加载第一个索引,而不加载其他内容,然后转到下一个JSON。对象单击图像键/值对,但我似乎无法使前面的下一个按钮正常工作。以下是我到目前为止的情况。。
PHP:
include('./connection.php');
$query = "SELECT artist, title, year, image, description, ID FROM voting";
$result = $mysqli->query($query);
if (!$result) die($mysqli->error);
$array = array();
while ($row = $result->fetch_object()) {
$array[] = $row;
}
echo json_encode(array('records' => $array), JSON_UNESCAPED_SLASHES);
JSON的格式如下:
{"records":[
{
"artist":"At The Drive In",
"title":"Acrobatic Tenement ",
"year":"1996",
"image":"./img/atdi01.jpg",
"description":"Arguably the the pioneering album of post-hardcore, Acrobatic Tenement is At The Drive In's seminal album. ",
"ID":"1"
}
]
剧本js文件:
$("#get-data").click(function(){ //load JSON data...
$.getJSON("api.php", //gets a JSON data from api.php
function(data){ //callback function if request succeeds
$.each(data.records, function(i,record){
var current = 0;
var img = $("<img/>").attr({
id: "album_image",
class: "album",
src: data.records[i].image,
});
$("#content").html(img);
$("#next-bt").click(function(){
current + 1;
return false;
});
$("#prev-bt").click(function(){
current - 1;
return false;
});
});
}); //end of callback function and end of .getJSON statement
}); //end of button.click function
请帮忙!我正要把头发拔出来。