开发者

get value customize of output json_encode() by jQuery.?

I insert data with json_encode() in database, want get just values name_units on row units in database. how is it?

This is output database in code php(by json_encode()):

my_table=>units=>name_units

[{"name":"jack","units":[{"name_units":"salam","price_units":"74开发者_运维问答,554","checkbox_units":["minibar","mobleman"]},{"name_units":"mokhles","price_units":"4,851,269","checkbox_units":["mobleman","tv"]},{"name_units":"fadat","price_units":"85,642","checkbox_units":["minibar","mobleman","tv"]}]}]

var dataObj = $(this).closest('form').serialize();

$.ajax({
    type: "POST",
    dataType: 'json',
    url: 'url',
    data: dataObj,
    cache: false,
    success: function(data) {
/////////////////////*HERE/////////////////////
        $.each(data, function(a, b) {
            //alert(b.units[name_units]);
            $('<p id="' + b.units[name_units] + '">' + b.units[name_units] + '</p>').appendTo('.class');
        });
/////////////////////HERE*/////////////////////
    };
})


Try this:

$.each(data, function(a, b) {
    $.each(b.units, function(c, d){
        $('<p id="' + d.name_units  + '">' + d.name_units + '</p>').appendTo('.class');
    });
});

Also, its a good idea to use jQuery templates for this scenario


units is an array, therefore you need to reference an item in the array or loop through all items in that array. b.units[0].name_units

Edit: like i said, you would have to loop through them.

$.each(b.units,function(i,unit){
  alert(unit.name_units);
});

you could also use a for loop if you would prefer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜