weixin_33743703 2016-10-05 14:55 采纳率: 0%
浏览 72

jQuery:附加到表头

So I'm trying to append data from the Url to the head of the table. The data called from the URL is a string list. So as you can see I'm trying to go through each value in the list and then append it to the head of my table, 'data'. But when I run the code nothing is appended. I know the data is going in because the window alert is displaying each of the strings.

JQuery:

$(document).ready(function () {
    $.ajax({
        url: 'http://..../api/Dynamic?table=table1',
        dataType: 'Json',
        success: function (tableResults) {
            var count = 0;
            $.each(tableResults, function () {
                window.alert(this);
                $('#data th:last-child').append('<th><a id="count">' + this + '</a></th>');
                count++;
            });
        }
    });
});

HTML:

<table id="data" border="1" align="center" width="95%">
        <thead>
            <tr>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

Any suggestions?

  • 写回答

1条回答 默认 最新

  • 零零乙 2016-10-05 15:08
    关注

    If the tableResults is an array of string you can use directly the each parameters and avoid the count variable like in the following snippet (use after instead of append because the new th elements must follow and not be child of the last th):

    //
    // if thead does not exist
    //
    if ($('#data thead').length == 0) {
      $('#data').append($('<thead/>'));
    }
    
    $.each(['email', 'address', 'country'], function (index, value) {
      if ($('#data th:last-child').length == 0) {
         //
         // if there are no th
         //
        $('#data thead').append('<th><a id="count' + index + '"</a>' + value + '</th>');
      } else {
        $('#data th:last-child').after('<th><a id="count' + index + '"</a>' + value + '</th>');
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
    <table id="data" border="1" align="center" width="95%">
      <thead>
        <tr>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?