我有点担心如何从jquery中的json数组中检索所有值。我知道它需要循环但是如何?功能 - 目前是:
$.ajax({
url: 'query.php',
data: "",
dataType: 'json',
success: ajaxfunction
});
function ajaxfunction(json_data){
// problem is below, i need to output all the data from the array
console.log (json_data)
while(json_data){
$('#maindisplay').html("<b>Product: </b>"+json_data.prod_name+"<b> colour: </b>"+json_data.colour); // problem is here, i need to output all the data from the array
}
}
php:
$result = mysql_query("SELECT * FROM fproduct WHERE fproduct.category='Shirts'");
$elements = array ();
do{
$row=mysql_fetch_array($result);
if ($row) //if there is anything
$elements[] = ($row);
} while($row);
echo json_encode($elements);
答案 0 :(得分:2)
你必须迭代数组的元素。
function ajaxfunction(json_data){
for (var i = 0; i < json_data.length; i++){
$('#maindisplay').append($("<b>Product: </b>"+json_data[i].prod_name+"<b> colour: </b>"+json_data[i].colour+"<br>"));
}
}