0

我有一个 jquery 函数如下:

$(document).on("click",".ball_link", function fetchLevels(){
     $.getJSON('fetch_level.php',{level: clicked_ball}, function(data) {
        $.each(data, function() {
        alert(data);
        });
    });
});

我的 fetch_level.php 文件如下所示:

$clicked_ball=$_GET["level"];
$sqlget="select * from level_flow where parent_level='$clicked_ball'";
$resultget=mysql_query($sqlget);

$response_array=array();
while($rowget=mysql_fetch_assoc($resultget)){
    $response_array[]=$rowget;
}

echo json_encode($response_array);

该查询返回跨 5 列的 3 行(所有整数)。我希望能够访问这 15 个值中的每一个,但是 js 代码中的 alert(data) 给出了这个:

([object],[Object]),([object],[Object]),([object],[Object])
4

2 回答 2

2

Treat as Object in jQuery, eg:

$(document).on("click",".ball_link", function fetchLevels(){
     $.getJSON('fetch_level.php',{level: clicked_ball}, function(data) {
        $.each(data, function(i, name) {
            alert(name.parent_level);
        });
    });
});

data contains all your database row names with values

于 2012-09-23T18:16:04.787 回答
1

Actually mysql_fetch_assoc returns an associative array, and you put that array inside another array

so in javascript you can try

alert(data[0].nameOfColumn);

You could iterate on it

$.each(data,function(i, el) {
    alert(el.nameOfColumn);
} );

in any case to see the structure of data you can use chrome or Firefox (with Firebug) and console.log(data)

于 2012-09-23T18:16:24.767 回答