使用 jquery 读取表值
read tabel value using jquery
早上好
我想尝试在 table 数据中使用 jquery 隐藏图标,如果 table 数据值被拒绝,则图标隐藏。
但好像不太对
<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 td 批准,批准,拒绝,拒绝
当我检查控制台时,它打印 Approve,Approve,Approve,Approve
按照HTML标准,您只能在页面中按时给出id,如果多次出现1个id,则必须使用find()。
<script>
$("tr").find("#status").each(function() {
var status = $(this).html();
if (status == 'reject'){
$(this).parent("tr").find("#edit").hide();
}
});
</script>
IDs must be unique 到 HTML DOM 树。
对于重复使用,我建议改用 类。
针对“拒绝”测试 status
的条件应该放在 each
循环中。
您正在测试每个元素以查看其状态是否为“拒绝”。
=
赋值而==
compares equivalence.
您需要从单击的 .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>
早上好 我想尝试在 table 数据中使用 jquery 隐藏图标,如果 table 数据值被拒绝,则图标隐藏。 但好像不太对
<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 td 批准,批准,拒绝,拒绝 当我检查控制台时,它打印 Approve,Approve,Approve,Approve
按照HTML标准,您只能在页面中按时给出id,如果多次出现1个id,则必须使用find()。
<script>
$("tr").find("#status").each(function() {
var status = $(this).html();
if (status == 'reject'){
$(this).parent("tr").find("#edit").hide();
}
});
</script>
IDs must be unique 到 HTML DOM 树。
对于重复使用,我建议改用 类。
针对“拒绝”测试 status
的条件应该放在 each
循环中。
您正在测试每个元素以查看其状态是否为“拒绝”。
=
赋值而==
compares equivalence.
您需要从单击的 .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>