数据不会显示在表格中,但会显示在 console.log 中

Posted

技术标签:

【中文标题】数据不会显示在表格中,但会显示在 console.log 中【英文标题】:Data won't show up in table but does in console.log 【发布时间】:2021-07-18 08:55:31 【问题描述】:

我的数据确实显示在 console.log 中,但实际上并没有显示在表中,我在这里做错了什么?

$.getJSON("http://localhost:3000/api/metatrader", function(data) console.log(data.result)

var table = document.getElementById('historyTable')

for (var i = 0; i < data.length; i++)
  var row = `<tr>
                <td>$data.result[i].order</td>
                <td>$data.result[i].symbol</td>
                <td>$data.result[i].size</td>
                <td>$data.result[i].profit</td>
                <td>$data.result[i].tp</td>
                <td>$data.result[i].sl</td>
            </tr>`
  table.innerhtml += row



);

【问题讨论】:

historyTable 是准确的 id 吗?你有错误吗? 【参考方案1】:

您想迭代 data.result,所以试试这个:

for (var i = 0; i < data.result.length; i++)

要插入行,请按照此示例中的说明进行操作:https://www.w3schools.com/jsref/met_table_insertrow.asp

【讨论】:

非常感谢,这是正确答案!

以上是关于数据不会显示在表格中,但会显示在 console.log 中的主要内容,如果未能解决你的问题,请参考以下文章