如何正确将数据追加到表中?

时间:2019-05-28 18:05:44

标签: javascript html

我想在html表中正确显示数据,包括我的js代码和html表。

  $.ajax({
             url : final,
             dataType: "json",
             type: 'GET',
             success : function (data) {
                    console.log(data.results.length);
                    var tr ,pr,cr,str;
                        for(var j = 0; j < data.results.length; j++){
                            cr = $('<tr>');
                            str =  "http://127.0.0.1:8000/chart/?varname="+ data.results[j].player_name;
                              $("#link").append(data.results[j].player_name)
                              cr.append('<td> <a href= " '+ str + ' "> '+ data.results[j].player_name+ "</a> </td>" )
                              $("#name").append(cr);
                           for(var i = 0; i < data.results[j].childtable_set.length; i++){
                                 tr = $('<tr>');
                                 tr.append('<td>' + data.results[j].childtable_set[i].goals+ "</td>" )
                                 $("#goals").append(tr);
                                 pr = $('<tr>');
                                 pr.append("<td>" + data.results[j].childtable_set[i].season+ "</td>" )
                                 $("#season").append(pr);

                    }
                  }
                 }
              });

<table class="table table-bordered" id='table'>
  <thead>
    <tr>
     <th id='name'>Player Name</th>
     <th id='season'>season</th>
     <th id='goals'>Goals scored</th>
    </tr>
    </thead>
  <tbody>
    <tr id ="re">

    </tr>
  </tbody>
</table>

我期望这样的事情:

ID |秒| div

1 | S1 | D1    | S2 | D2

| S3 | D3

2 | S3 | D3    | S4 | D4    | S5 | D5

这是实际结果:

             season          Goals scored
               2017               0
               2014               0
               2015               4 
               2016               5
               2017               7
               2018               7 
               2014               9
               2015               2
               2016               1
Player Name    2017               0 
Omar Ramos     2018               4
Sergio Ramos   2014               6

0 个答案:

没有答案