也许是一个技巧问题。我正在尝试通过 ajax/json 提取数据来制作高尔夫记分卡。
这是我获得记分卡的方法。
php:
$scorecard = array();
$players = array();
$data = array();
$result = mysqli_query($con,"SELECT * FROM ga_turndata WHERE fk_turnid = 1 ORDER BY hole_no ASC LIMIT 18");
while($row = mysqli_fetch_array($result)){
$scorecard[] = array('hole' => $row['hole_no'], 'par' => $row['par'], 'hcp' => $row['hcp']);
}
$players[] = array('user' => 'Jane Doe', 'id' => '89', 'useraStrokes' => '5');
$players[] = array('user' => 'Jemaine Dice', 'id' => '245', 'useraStrokes' => '17');
$players[] = array('user' => 'Jimmie Gallon', 'id' => '16', 'useraStrokes' => '13');
$data = array('scorecard' => $scorecard, 'players' => $players);
在 stackoverflow 用户的帮助下,我让它通过 json 发送 2 个不同的数组。
我得到这样的数据:
$.getJSON('http://mypage.com/json/scorecard.php', function(data) {
var array = [];
var c = 0;
$.each(data.players, function(i, player){
eval("player" + i + " = player.user");
c++;
});
$.each(data.scorecard, function (i, scorecard) {
array.push($("#theholes").append("<li><div class='holeinfo'>"+scorecard.hole+"</div><div id='players'>Here i want to dynamicly add players from the first $.each</div></li>"));
});
});
获得 18 洞是没有问题的,但不知道如何将球员动态地附加到 $each 循环中?
希望这是有道理的!?!希望提前帮助和感谢:-)