繁体   English   中英

在jQuery中输出json数组

[英]Output json array in jQuery

我有点担心如何从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);

您必须迭代数组的元素。

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>"));
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM