【发布时间】:2022-01-10 10:54:41
【问题描述】:
我有这个:
<thead class="thead-light">
<tr>
<th>Date (YYYY/MM/DD)</th>
<th>Exercise</th>
<th>WEIGHT</th>
<th>REPS</th>
<th>DURATION</th>
<th id="showclientusername">CLIENT USERNAME</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% if (data.length) {
for(var i = 0; i < data.length; i++) { %>
<% if (data.length <=4) { %>
<style>
#vact {
height: 200px;
}
</style>
<% } %>
<tr>
<td><%=data[i].DATE%></td>
<td><%=data[i].EXCERCISE%></td>
<td><%=data[i].WEIGHT%></td>
<td><%=data[i].REPS%></td>
<td><%=data[i].DURATION%></td>
<td id="isusername"><%=data[i].USERNAME%></td>
<script>
if ('<%= clientUsername %>' == '<%= trainerUsername %>') {
$('#isusername').css("visibility", "visible");
console.log("it is equal!")
} else {
$('#isusername').css("display", "none")
$('#showclientusername').css("display", "none")
console.log("false")
}
</script>
然后发生的情况是,当我点击特定页面时,<%= clientUsername %> 和 <%= trainerUsername %> 不相等,这很好,没错,它会记录 false。但是,我认为这是因为我覆盖了id,它第一次遍历循环(表中的第一行)时,它隐藏了所有应该做的事情,并且做到了:$('#isusername').css("display", "none") 完美。但随后它会在循环中运行第二次、第三次、第四次等时间,实际上是显示这些值。所以第一个总是隐藏的,但之后每次都不会隐藏。
我认为这与我正在覆盖<td id="isusername" 的事实有关,因为我处于循环中,但不知道如何修复它。提前感谢您的帮助
【问题讨论】:
-
无关:你为什么在 for 循环中多次写
<style>#vact { height: 200px }</style>? -
您还将得到包含
td和isusernameid 的多行。 ID 应该是唯一的。 -
@J.Titus 是的,这就是我认为我遇到的问题。那么有什么办法可以解决这个问题吗?
标签: javascript ejs