1

$.post() を使用して json の結果を取得し、結果ごとにリストに出力できるようにしたいと考えています。

以下を使用すると、最後のjsonアイテムのみが表示されます

Jクエリ

$.post('/assets/inc/account-info.php', qString, function (data) {
    console.log(data);
    var datas = jQuery.parseJSON(data);
    console.log(datas);
    $("#note").append("<li>id=recordsarray_"+datas.id+"><a href='#"+datas.id+"'>"+datas.name+"</a></li>");  
});
4

4 に答える 4

2

インデックスでデータをループして設定し、それぞれの ID と名前を取得します。また、(リストと言ったので)データに複数のレベルがあると想定しています。

$.each(datas, function() {
    $.each(this, function(key, value) {
        $("#note").append("<li id=recordsarray_" + 
             datas[key].id + "><a href='#" + 
             datas[key].id + "'>" + 
             datas[key].name + "</a></li>"
        );
    });
});
于 2012-08-30T14:37:17.727 に答える
2

フロントエンドでオブジェクトの配列を取得しており、このオブジェクトにはIDと名前が含まれていると想定しています:

// To put data directly in html you can do this :
    $.each(datas,function(i,data){
       $("#note").append("<li>id=recordsarray_"+data.id+"><a href='#"+data.id+"'>"+data.name+"</a></li>");
    });

 OR

// To Put your data in array you can do this :
    var arr = [];
    // This will help you putting the json object in to array
    $.each(datas,function(i,data){
       arr.push({id:data.id, name:data.name});   
    });

    // once you get the array you can loop through it and add it to your html:
    for (i=0;i<arr.length;i++){ 
        $("#note").append("<li>id=recordsarray_"+arr[i].id+"><a href='#"+arr[i].id+"'>"+arr[i].name+"</a></li>");
    });   
    }
于 2012-08-30T15:02:31.100 に答える
1
$(datas).each(function(){ $("#note").append("<li>id=recordsarray_"+this.id+"><a href='#"+this.id+"'>"+this.name+"</a></li>"); });
于 2012-08-30T14:37:29.720 に答える
1

これを試して

$.each(datas, function(i,item){
    $("#note").append("<li>id=recordsarray_"+datas[i].id+"><a href='#"+datas[i].id+"'>"+datas[i].name+"</a></li>");
});
于 2012-08-30T14:40:49.303 に答える