【问题标题】:Django + JQuery - Iterate over table rows updating each row with json dataDjango + JQuery - 遍历表行,用 json 数据更新每一行
【发布时间】:2019-11-04 11:03:46
【问题描述】:

我的表格行看起来像这样

{% for item in items %}
    <tr id="labels" data-index="{{ forloop.counter }}"> </tr>  
{% endfor %}

我有这个 json 数据:

"labels": [
    "A",
    "B",
    "C ",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z"
],

我正在尝试同时迭代表行和 json 数据,以便我可以为每个表行分配其标签。

jQuery.each(labels, function() {
newlabel = this;
document.querySelectorAll('#labels').forEach(function (element, index) {
element.innerHTML = newlabel;
});
})},

但是这样一来,所有行都填充了字母 Z,而不是按字母顺序排列的 A-Z。

感谢您的帮助

【问题讨论】:

    标签: jquery django


    【解决方案1】:

    对于任何感兴趣的人,这里是解决方案:

    var iLabel = 0;
    document.querySelectorAll('#labels').forEach(function (element, index) {
    element.innerHTML = iLabel < labels.length ?
    labels[iLabel] :
    'undefined';
    iLabel++;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2019-04-03
      • 2016-06-21
      相关资源
      最近更新 更多