使用jquery读取表值



早上好我想用jquery在表格数据中隐藏一个图标,如果表格数据值拒绝,那么图标隐藏。但这似乎不对

<tr>            
<td id="status"><?php echo $row[PO_Status]; ?></td>
<td>
<center>
<a id="edit" href="./page.php?page=editdatapo&id=<?php echo $row[id]; ?>">
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-pencil"></span>
</button>
</a>
</center>
</td>
</tr>
<script>
$("#status").each(function() {
var status = $(this).text();
console.log(status);
});
if (status = 'reject'){
$("#edit").hide();
}
</script>

我有4个选项:批准、批准、拒绝、拒绝当我检查控制台时,它打印Approve,Approve,Approve,Approve

根据HTML标准,你只能在一个页面中定时给出id,如果有多个id,那么你必须使用find()。

<script>
$("tr").find("#status").each(function() {
var status = $(this).html();
if (status == 'reject'){
$(this).parent("tr").find("#edit").hide();
}
});
</script>

id必须是唯一的HTML DOM树。
对于经常使用,我建议使用类。

检验status是否"不合格品"的条件;应该进入each循环。
您正在测试每个元素以查看其状态是否为"拒绝"。

=赋值,==比较等价。

您需要从单击的.status遍历到下一个单元格中与其相关的.edit元素。我通过移动到最近的<tr>,然后在该行中搜索.edit元素来做到这一点。

$('.status').each(function() {
let $this = jQuery(this);
let status = $this.text();
console.log(status);
if (status == 'reject') {
$this.closest('tr').find('.edit').hide();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td class="status">one</td>
<td>
<center>
<a class="edit" href="./page.php?page=editdatapo&id=1">edit</a>
</center>
</td>
</tr>
<tr>
<td class="status">reject</td>
<td>
<center>
<a class="edit" href="./page.php?page=editdatapo&id=2">edit</a>
</center>
</td>
</tr>
<tr>
<td class="status">three</td>
<td>
<center>
<a class="edit" href="./page.php?page=editdatapo&id=3">edit</a>
</center>
</td>
</tr>
</tbody>
</table>

最新更新