简体   繁体   中英

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*/////////////////////
    };
})

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM