如何动态地为Jquery中的表项分配id

时间:2017-09-22 07:29:40

标签: jquery html jquery-ui



success: function(response) {
  console.log(response);


  for (var i = 0; i < response.length; i++) {
    var trHTML = '';
    for (var j = 0; j < response[i].Products.length; j++) {

      trHTML += '<tr class="clickable-row" data-href="index.html"><td>' + response[i].IP + '</td><td>' + response[i].Products[j].Product + '</td><td>' + response[i].Products[j].CVECount + '</td></tr>';
      // console.log(response[i].Products[j].Product);
      console.log(trHTML);

    }
    $('#ProductsTableBody').append(trHTML);
  }

  $('.js-exportable').DataTable({
    dom: 'Bfrtip',
    responsive: true,
    buttons: [
      'copy', 'csv', 'excel', 'pdf', 'print'
    ]
  });


},
&#13;
&#13;
&#13;

我有这个代码。我想动态地为每个id分配唯一的id。而且我想让它们成为可点击的表格项目。在他们根据他们的id点击后,我从后端获取另一个信息并指导另一个页面。如何动态地为表项分配id并使其可单击。

1 个答案:

答案 0 :(得分:1)

使用<tr>属性创建这样的动态id,如下所示:

trHTML += '<tr class="clickable-row" id="id_'+i+'_'+j" data-href="index.html"><td>' + response[i].IP + '</td><td>' + response[i].Products[j].Product + '</td><td>' + response[i].Products[j].CVECount + '</td></tr>';
相关问题