无法访问除第一行以外的其他行的获取值

Unable to access get values of other rows other than the first row

我有一个 json 人物对象。我已经成功地将这些对象放置在 table 中。我的 objective 是在每一行放置按钮并访问每一行的人。在每一行上创建一个按钮是容易的部分,但是让按钮与该行上的内容相对应是困难的。有什么建议吗?我是网络开发新手

我已经尝试在每一行上放置一个按钮,希望代码能够知道它所在行上的内容。此外,奇怪的是,当我单击 table 中的第一个按钮时,它 returns 给我正确的名称,但是当我单击 table 中的第二个按钮时,它没有 console.log 随便什么..

    div(style = "text-align: center;")
                table.table.table-striped.table-dark
                    thead
                        tr
                            th(scope='col')
                            th(scope='col') Friend
                            th(scope='col') Email
                    tbody
                        each item in json
                            tr
                                td
                                    button(id = 'sendRequest', type = 'submit', value = '#{item.name}', name = 'name')
                                td #{item.name}
                                td #{item.email}

我希望在单击该行上的按钮时获得该行上特定人员的姓名

id 被认为是 独一无二的 。您可以尝试为每个按钮提供一个唯一的生成的 iddata- 属性,例如 B1B2B3,可用于确定按钮中的哪一行JSON 它要访问的数据库。

在生成元素时,将 id 设置为 = "El" + index 之类的内容。然后您可以使用 ... document.getElementById("El" + index).

访问它