私はstackoverflowから検索し、次のjqueryコードを実装しました。これは、phpからjsonオブジェクトを取得し、それをliタグとして追加しますが、1つの値の3つのインスタンスを追加します。 サンプルコードは次のとおりです。
var $childs = $('.childs');
$.getJSON('common/db/fetchUniversity.php', function(data) {
$(data).each(function() {
var $element = $childs.clone().removeClass('childs').appendTo('#colleges');
$element.attr('id', this.id+"u");
$element.html("<a href='#' class='img'><strong><img alt='image' src='images/mit.jpg' style='height:40px; width:40px;' /></strong></a><div style='padding-right: 30px;'><h4 style='display:inline; margin:1px;'>"+this.name+"</h4><p style='display:inline;'>"+this.description+"</p></div><p style='margin:2px;'><a href='#' style='text-decoration: none;'><span class='ui-icon ui-icon-circle-plus' style='display:inline-block; vertical-align:middle;'></span>Follow</a></p><ul class='actions'><li class='remove'><a href='javascript:RemoveMe('#"+this.id+"u')'><span class='ui-icon ui-icon-closethick'>close</span></a></li></ul>");
});
});
$.getJSON('common/db/fetchPeople.php', function(data1) {
$(data1).each(function() {
var $element = $childs.clone().removeClass('childs').appendTo('#people');
$element.attr('id', this.id+"p");
$element.html("<a href='#' class='img'><strong><img alt='image' src='"+btoa(this.pic)+"' style='height:40px; width:40px;' /></strong></a><div style='padding-right: 30px;'><h4 style='display:inline; margin:1px;'>"+this.name+"</h4><p style='display:inline;'>"+this.job_desc+","+this.location+"</p></div><p style='margin:2px;'><a href='#' style='text-decoration: none;'><span class='ui-icon ui-icon-circle-plus' style='display:inline-block; vertical-align:middle;'></span>Follow</a></p><ul class='actions'><li class='remove'><a href='javascript:RemoveMe('#"+this.id+"p')'><span class='ui-icon ui-icon-closethick'>close</span></a></li></ul>");
});
});
$.getJSON('common/db/fetchGroups.php', function(data) {
$(data2).each(function() {
var $element = $childs.clone().removeClass('childs').appendTo('#groups');
$element.attr('id', this.id+"g");
$element.html("<a href='#' class='img'><strong><img alt='image' src='images/groups/hbr.jpg' style='height:40px; width:40px;' /></strong></a><div style='padding-right: 30px;'><h4 style='display:inline; margin:1px;'>"+this.name+"</h4><p style='display:inline;'>"+this.job_desc+","+this.location+"</p></div><p style='margin:2px;'><a href='#' style='text-decoration: none;'><span class='ui-icon ui-icon-circle-plus' style='display:inline-block; vertical-align:middle;'></span>Follow</a></p><ul class='actions'><li class='remove'><a href='javascript:RemoveMe('#"+this.id+"g')'><span class='ui-icon ui-icon-closethick'>close</span></a></li></ul>");
});
});
上記のコードは3つのjsonオブジェクトをフェッチし、それをliタグに追加していますが、上記のすべては単一値の3つのインスタンスを追加しています。liタグはjqueryアコーディオンの内部にあります。
json phpファイルをヒットすると、正しく返されますが、jqueryコードが何か間違ったことをしていると思います。どこが悪いのか教えてください。
ブラウザに入力した場合のjsonオブジェクト:-
[{"id":1,"name":"Stanford university","description":"One of the best university in the world"},{"id":2,"name":"Princeton University","description":"One of the best university in the world"},{"id":3,"name":"Yale University","description":"One of the best university in the world"},{"id":4,"name":"California University","description":"One of the best university in the world"},{"id":5,"name":"Yale University","description":"One of the best university in the world"},{"id":6,"name":"California University","description":"One of the best university in the world"},{"id":7,"name":"Princeton University","description":"One of the best university in the world"},{"id":8,"name":"Stanford university","description":"One of the best university in the world"},{"id":9,"name":"California University","description":"One of the best university in the world"},{"id":10,"name":"Princeton University","description":"One of the best university in the world"},{"id":11,"name":"Yale University","description":"One of the best university in the world"}]
上記のid:1の値は3回追加され、すべての値で発生しています。