简体   繁体   English

如何循环唯一的目标ID? [jQuery]

[英]How to loop unique target ID? [jQuery]

How to assign unique target ID into individual row of JSON data?如何将唯一的目标 ID 分配给 JSON 数据的单独行? In this case, all row using the same name which is data-target=".childData" .在这种情况下,所有行都使用相同的名称,即data-target=".childData" How to assign different incremental name such as .childData , .childData2 , .childData3 and so on in jQuery etc?如何在 jQuery 等中分配不同的增量名称,例如.childData.childData2.childData3等?

$.post('requestForm_table.php', function (data) {
  $.each(data, function (i, item) {
    //calStatus = item.status;

    $("#formAll").append("<tr id=\"highlight\" data-toggle=\"collapse\" data-target=\".childData\">" +
      "<td>" + item.id + "</td>" +
      "<td>" + item.ein_number + "</td>" +
      "<td>" + item.instrumentDesc + "</td>" +
      "<td>" + item.dateReceived + "</td>" +
      "<td>" + item.siteCode + "</td>" +
      "<td class=\"text-success text-center\">" + "<span class=\"statusColor\" style=\" font-style: italic;\">" + item.status + "</span>" + "</td>" +
      "</tr>" +
      "<tr>" +
      "<td colspan=6 class=\"hiddenRow\">" +
      "<div class=\"collapse childData ml-5\" style=\"font-size: 13px\">" +
      "<div class=\"row\">" +
      "<div class=\"col-4\">" + "Calibration Job: " + item.status + "</div>" +
      "<div class=\"col-4\">" + "Owner: " + item.owner + "</div>" +
      "<div class=\"col-4\">" + "Serial Number: " + item.serialNum + "</div>" +
      "</div>" +
      "<div class=\"row\">" +
      "<div class=\"col-4\">" + "Date Received:  " + item.dateReceived + "</div>" +
      "<div class=\"col-4\">" + "Calibration Cost: " + item.calCost + "</div>" +
      "<div class=\"col-4\">" + "Manufacturer: " + item.manufacturer + "</div>" +
      "</div>" +
      "</div>" +
      "</td>" +
      "</tr>"
    );
  });
});

$.each(data, function(i, item) : here i is actually the index of your array. So you can add this index when generating the html. $.each(data, function(i, item) :这里的i实际上是你的数组的索引。所以你可以在生成 html 时添加这个索引。

Replace代替

$("#formAll").append("<tr id=\"highlight\" data-toggle=\"collapse\" data-target=\".childData\">"

with this-有了这个-

$("#formAll").append("<tr id=\"highlight"+i+"\" data-toggle=\"collapse\" data-target=\".childData"+i+"\">"

Mahbub Moon solution should work. Mahbub Moon 解决方案应该有效。 But it's not recommended to use a same id for several html components.但不建议对多个 html 组件使用相同的 id。

You should replace你应该更换

$("#formAll").append("<tr id=\"highlight\" data-toggle=\"collapse\" data-target=\".childData\">"

by经过

$("#formAll").append("<tr id=\"highlight" + i + "\" data-toggle=\"collapse\" data-target=\".childData"+i+"\">"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM