使用Json从函数中添加和返回值

时间:2015-06-19 03:15:51

标签: jquery ajax json

我有使用ajax和json从表查看中获取一些值的函数。我的json工作。它提供了我需要的价值。现在,我想添加trtd中的所有值并返回它。但它给出了''或空白值。我希望有解决方案:)

function get_review(id_jurnal){
var row= '';
var Example = (function () {
    function self() { } 
    self.Request = function (params) {
        $.ajax({
            data: "idjurnal_review="+id_jurnal,
            url:  "php/ambildata.php",
            cache: false,
            async: true,
            success: function ($json) {
                params.success($json);
            }
        });
  };
    return self;
})($);

var locale = Example.Request({
    async: true, // Set to false to enable synchronized calls
    url: "php/ambildata.php",
    success: function ($json) {
        locale = $json;

        var response = eval("(" + $json + ")");
        for(i=0;i < response.messages.pesan.length; i++) {
        id_review = response.messages.pesan[i].id_review;
        tglmulai_review = response.messages.pesan[i].tglmulai_review;
        tglselesai_review = response.messages.pesan[i].tglselesai_review;
        file_review = response.messages.pesan[i].file_review;

        var data = '<tr>'+
            '<td width="20%">Tgl Penunjukan</td>'+
            '<td width="1%">:</td>'+
            '<td>'+tglmulai_review+'</td>'+
        '</tr>';

        row = row + data;
        }
    }
});

return row; 
};

var id_jurnal = 1;
get_review(id_jurnal);
return '<table cellpadding="5" cellspacing="0" border="0" class="redshown">'+get_review(id_jurnal)+'</table>';

1 个答案:

答案 0 :(得分:0)

刷新大脑后我得到了解决方案:D 这就是我的所作所为。 首先我先制作桌子。

功能格式(d){

return '<table cellpadding="5" cellspacing="0" border="0" class="redshown"><tr id="tabelbaru"></tr></table>'; }

然后我通过ajax json

调用数据
function get_review(id_jurnal){
var Example = (function () {
    function self() { } 
    self.Request = function (params) {
        $.ajax({
            data: "idjurnal_review="+id_jurnal,
            url:  "php/ambildata.php",
            cache: false,
            async: true,
            success: function ($json) {
                params.success($json);
            }
        });
  };
    return self;
})($);

var locale = Example.Request({
    async: true, // Set to false to enable synchronized calls
    url: "php/ambildata.php",
    success: function ($json) {
        locale = $json;

        var response = eval("(" + $json + ")");
        for(i=0;i < response.messages.pesan.length; i++) {
        id_review = response.messages.pesan[i].id_review;
        tglmulai_review = response.messages.pesan[i].tglmulai_review;
        tglselesai_review = response.messages.pesan[i].tglselesai_review;
        file_review = response.messages.pesan[i].file_review;
        idjurnal_review = response.messages.pesan[i].idjurnal_review;

        var row = $(document.createElement('tr')).attr("id", 'DivTambah' + idjurnal_review);
        row = '<tr>'+
            '<td width="20%">Tgl Penunjukan</td>'+
            '<td width="1%">:</td>'+
            '<td>'+tglmulai_review+'</td>'+
        '</tr>';
        $(row).insertBefore("#tabelbaru");
        }
    }

}); };

使用javascript插入tr和td。 谢谢大家:)