我一直在尝试通过jQuery从表行中获取数据,但未返回任何内容。我到处寻找了多个教程,但没有找到任何可以帮助我的东西。
这是我的代码:
jquery:
function markAsRead(checkbox) {
if (checkbox.checked) {
var current_row = $('#msg-table').closest("tr");
var column1 = current_row.find("td:eq(0)").text();
var column2 = current_row.find("td:eq(1)").text();
var column3 = current_row.find("td:eq(2)").text();
var column4 = current_row.find("td:eq(3)").text();
var data = column1 + " " + column2 + " " + column3 + " " + column4;
console.log(data);
} else {
return;
}
}
php和html:
<table class="w3-table-all" id="msg-table">
<thead>
<tr>
<th>From</th>
<th>Subject</th>
<th>Message</th>
<th>Date Sent</th>
<th></th>
</tr>
</thead>
<?php
foreach ($this->paginator as $value):
if ($value->active == 1):
?>
<tr class="w3-hover-text-red w3-text-black">
<td>
<b><?php echo $value->from; ?></b>
</td>
<td>
<b><?php echo $value->subject; ?></b>
</td>
<td>
<b><?php echo $value->message; ?></b>
</td>
<td>
<b><?php echo $value->date_sent; ?></b>
</td>
<td>
<input type="checkbox" class="w3-check" onchange='markAsRead(this);' id="<?php echo $value->id; ?>"> Mark As Read
</td>
</tr>
<?php else: ?>
<tr class="w3-hover-text-red w3-text-black">
<td>
<?php echo $value->from; ?>
</td>
<td>
<?php echo $value->subject; ?>
</td>
<td>
<?php echo $value->message; ?>
</td>
<td>
<?php echo $value->date_sent; ?>
</td>
<td>
<input type="checkbox" class="w3-check" onchange='markAsUnread(this);' id="<?php echo $value->id; ?>"> Mark As Unread
</td>
</tr>
<?php
endif;
endforeach;
?>
</table>
以下是我遇到的问题的屏幕截图:
任何帮助将不胜感激,因为我在这个问题上停留了几天,并想找出解决方法。
答案 0 :(得分:2)
使用$(checkbox).closest("tr");
查找复选框的父级
function markAsRead(checkbox) {
console.clear();
if (checkbox.checked) {
var current_row = $(checkbox).closest("tr");
var column1 = current_row.find("td:eq(0)").text();
var column2 = current_row.find("td:eq(1)").text();
var column3 = current_row.find("td:eq(2)").text();
var column4 = current_row.find("td:eq(3)").text();
var data = column1 + " " + column2 + " " + column3 + " " + column4;
console.log(data);
} else {
return;
}
}