0

我收到以下 JSON 对象:

[Object, Object, Object]
0: Object
a: "a"
b: "b"
c: "c"
__proto__: Object
1: Object
a: "a"
b: "b"
c: "c"
__proto__: Object
2: Object
a: "a"
b: "b"
c: "c"
__proto__: Object
length: 3
__proto__: Array[0]

使用以下 ajax 代码:

function describeItems() {

 var html;
 var loadingContainer = $("<div class='containerLoading'></div>");
 var emptyContainer = $('<div class="emptyContainer"><div style="width: 400px; margin-left:20px;"><h4>Let\'s get started!</h4>You do not have anything in your bucket.<br \>Click the shop button to start shopping items.<button style="margin-top: 20px;" class="btn btn-large btn-primary" type="button" data-toggle="modal" data-target="#shop-items">Shop Items</button></div></div>');
 $("#items-table").append(loadingContainer.fadeIn(100));

 $.ajax( { 
  type: "POST", 
  url: "function.php", 
  data: {action: 'test'}, 
  dataType: "json", 
  cache: "false",
  success: function(data) { 

  // DEBUG
  console.log(data)
  if (data != null)
  {
      // DEBUG
      //console.log(data);
      html = html + '<tr>';
      html = html + '<td><input type="checkbox" value=""></td>';

      $.each(data, function(key,value) {
       if (!$.isPlainObject(value)) {
        html = html + '<td>' + value + '</td>'; 
       };
      });

     html = html + '</tr>';

     $.each(data, function(key,value) {  
      html = html + '<tr>';
      html = html + '<td><input type="checkbox" value=""></td>';

      if ($.isPlainObject(value)) { 

       // DEBUG   
       //console.log(value);
       $.each(data[key], function(key, value) {
        html = html + '<td>' + value + '</td>';     
       }); 
      };

     html = html + '</tr>';
     });

     html = html + '</tbody>';

     // ADD THE HTML TO THE DIV.
     $('#innerContent').html(html);

     // WHEN LOADING IS DONE, REMOVE OVERLAY.
     $("#item-table .containerLoading").fadeOut(100, function() {
      $(this).remove();
     }); 
 }
 else
 {
     // WHEN LOADING IS DONE, REMOVE OVERLAY.
     $("#item-table .containerLoading").fadeOut(100, function() {
      $(this).remove();
     });

    $("#item-table").append(emptyContainer.fadeIn(100));

 }
     }, // END OF SUCCESS
     error: function (xhr, ajaxOptions, thrownError) {
      alert('Response Code: ' + xhr.status);
      alert(thrownError);
      alert(xhr.responseText);
     }

 }); // END OF $.AJAX 
}; 

但不知何故,我的 html 表中有一个空<tr>的,至少它没有对象内容..:

<table class="table table-striped table-condensed">
    <thead>
        <tr>
            <th></th>
            <th>a</th>
            <th>b</th>
            <th>c</th>
        </tr>
    </thead>
    <tbody id="innerContent">
    <tr>
        <td>
            <input type="checkbox" value="">
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" value="">
        </td>
        <td>
            a
        </td>
        <td>
            b
        </td>
        <td>
            c
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" value="">
        </td>
        <td>
            a
        </td>
        <td>
            b
        </td>
        <td>
            c
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" value="">
        </td>
        <td>
            a
        </td>
        <td>
            b
        </td>
        <td>
            c
        </td>
    </tr>       
    </tbody>
</table>

我需要改变什么来删除第一个空的<tr>

4

1 回答 1

1

这个循环不会产生任何TD其他东西checkbox

  html = html + '<tr>';
  html = html + '<td><input type="checkbox" value=""></td>';

  $.each(data, function(key,value) {
   if (!$.isPlainObject(value)) {
    html = html + '<td>' + value + '</td>'; 
   };
  });

 html = html + '</tr>';

您的数据是一个数组,数组的每个元素都是一个对象。

if (!$.isPlainObject(value))总是错误的,但无论如何你都在创建行

于 2012-12-26T23:57:34.547 回答