警报变量时此代码有效,但如果没有则无效

时间:2013-07-17 04:26:36

标签: javascript jquery html json native

<script type="text/javascript">
    function get_test_json(){
        var HTML = "";
        var ClientHTML = "";
        var nomor = 1;
        HTML = '<table data-role="table" id="table-custom-2" data-mode="columntoggle" class="ui-body-d ui-shadow table-stripe ui-responsive" data-column-btn-theme="a" data-column-btn-text="Pilih Kolom" data-column-popup-theme="a">';
        HTML += "<thead>";
        HTML += '<tr class="ui-bar-a">';
        HTML += '<th data-priority="2">No</th>';
        HTML += '<th data-priority="3">Foto</th>';
        HTML += '<th>Nama</th>';
        HTML += '<th data-priority="5">Keterangan</th>';
        HTML += '</tr>';
        HTML += '</thead>';
        HTML += '<tbody>';
        $.getJSON("http://localhost/JSON_test/my_json_list.php", function(person){
            $.each(person, function(key, value){
                $.each(value, function(names, specs){
                    ClientHTML += '<tr>'; 
                    ClientHTML += '<th>'+nomor+'</th>'; 
                    ClientHTML += '<td><img src="" alt="" height="" width=""></td>'; 
                    ClientHTML += '<td>'+names+'</td>'; 
                    ClientHTML += '<td><a href="http://localhost/4r.mobile.kemensos/profil_peneliti.html">'+specs+'</a></td>'; 
                    ClientHTML += '</tr>';
                    nomor = nomor+1;
                });
            });
        });
        alert(ClientHTML); // this is the problem
        HTML += ClientHTML;
        HTML += '</tbody>';
        HTML += '</table>';
        document.getElementById("test").innerHTML = HTML;
    }
    </script>

当我删除警报时,ClientHTML变量为null。
我很抱歉我的英文不好和一些转储代码,我在这里是新手 谢谢你的帮助

2 个答案:

答案 0 :(得分:1)

这是警报和非警报的问题---- 时间问题

 $.getJSON("http://localhost/JSON_test/my_json_list.php", function(person){
            $.each(person, function(key, value){
                $.each(value, function(names, specs){
                    ClientHTML += '<tr>'; 
                    ClientHTML += '<th>'+nomor+'</th>'; 
                    ClientHTML += '<td><img src="" alt="" height="" width=""></td>'; 
                    ClientHTML += '<td>'+names+'</td>'; 
                    ClientHTML += '<td><a href="http://localhost/4r.mobile.kemensos/profil_peneliti.html">'+specs+'</a></td>'; 
                    ClientHTML += '</tr>';
                    nomor = nomor+1;
                     HTML += ClientHTML;
                   HTML += '</tbody>';
                   HTML += '</table>';
                  document.getElementById("test").innerHTML = HTML;
                });
        });
    });

完成ajax请求后,您已完成了操作。

答案 1 :(得分:0)

我相信这会奏效。只需将它们分成两个功能即可。

<script>
function get_test_json(){
  var nomor = 1;
  var ClientHTML = "";
  $.getJSON("http://localhost/JSON_test/my_json_list.php", function(person){

        $.each(person, function(key, value){
            $.each(value, function(names, specs){
                ClientHTML += '<tr>'; 
                ClientHTML += '<th>'+nomor+'</th>'; 
                ClientHTML += '<td><img src="" alt="" height="" width=""></td>'; 
                ClientHTML += '<td>'+names+'</td>'; 
                ClientHTML += '<td><a href="http://localhost/4r.mobile.kemensos/profil_peneliti.html">'+specs+'</a></td>'; 
                ClientHTML += '</tr>';
                nomor++;
            });
        });


        get_next(ClientHTML);
    });


}

function get_next(ClientHTML){
    var HTML = "";
    HTML += '<table data-role="table" id="table-custom-2" data-mode="columntoggle" class="ui-body-d ui-shadow table-stripe ui-responsive" data-column-btn-theme="a" data-column-btn-text="Pilih Kolom" data-column-popup-theme="a">';
    HTML += "<thead>";
    HTML += '<tr class="ui-bar-a">';
    HTML += '<th data-priority="2">No</th>';
    HTML += '<th data-priority="3">Foto</th>';
    HTML += '<th>Nama</th>';
    HTML += '<th data-priority="5">Keterangan</th>';
    HTML += '</tr>';
    HTML += '</thead>';
    HTML += '<tbody>';

    HTML += ClientHTML;
    HTML += '</tbody>';
    HTML += '</table>';
    document.getElementById("test").innerHTML = HTML;

    $('#table-custom-2').table('refresh');
}
</script>