Javascript JQuery在一个表行中获取多个表数据的每个值
嗨,我有一张有多行的桌子我想知道如何获取该表每行中的特定数据。在该表中,我需要获取学号数据及其附带的成绩Javascript JQuery在一个表行中获取多个表数据的每个值,javascript,jquery,codeigniter,Javascript,Jquery,Codeigniter,嗨,我有一张有多行的桌子我想知道如何获取该表每行中的特定数据。在该表中,我需要获取学号数据及其附带的成绩 <tbody> <?php foreach ($class_list_view as $student) { ?> <tr> <td class="studentnumber"><?= $student->studentnumber ?></td> <td><?
<tbody>
<?php foreach ($class_list_view as $student) { ?>
<tr>
<td class="studentnumber"><?= $student->studentnumber ?></td>
<td><?= $student->lastname ?></td>
<td><?= $student->firstname ?></td>
<td><?= $student->middlename ?></td>
<td><?= $student->level ?></td>
<td><?= $student->year ?></td>
<td>
<select class="custom-select grade" style="height: 20px;">
<option selected value="">Select Grade</option>
<option value="passed">Passed</option>
<option value="failed">Failed</option>
</select>
</td>
</tr>
<?php } ?>
</tbody>
但是在
等级
索引中,它只取第一个。它应该在每一行中输入select的值,类似于tdstudentnumber
您可以通过行而不是单元格循环
var studentnum_array = [];
$("table tbody tr").each(function(index) {
studentnum_array.push({
"studentnumber": $(this).find('td.studentnumber').text(),
"grade": $(this).find('select.grade').val()
});
});
console.log(studentnum_array);
如果要在单元格中循环,必须找到与相应的studentnumber
单元格相关的select
var studentnum_array = [];
$("table tbody td.studentnumber").each(function(index) {
studentnum_array.push({
"studentnumber": $(this).text(),
"grade": $(this).closest('tr').find('select.grade').val()
});
});
console.log(studentnum_array);
您可以通过行而不是单元格循环
var studentnum_array = [];
$("table tbody tr").each(function(index) {
studentnum_array.push({
"studentnumber": $(this).find('td.studentnumber').text(),
"grade": $(this).find('select.grade').val()
});
});
console.log(studentnum_array);
如果要在单元格中循环,必须找到与相应的studentnumber
单元格相关的select
var studentnum_array = [];
$("table tbody td.studentnumber").each(function(index) {
studentnum_array.push({
"studentnumber": $(this).text(),
"grade": $(this).closest('tr').find('select.grade').val()
});
});
console.log(studentnum_array);
由于您已经在每个
td
中循环,因此您需要找到它所包含的tr
,然后查找选择
$("table tbody tr td.studentnumber").each(function (index) {
var grade = $(this).closest('tr').find('select.grade').val();
studentnum_array.push( {"studentnumber": $(this).text(), "grade": grade } );
});
console.log( studentnum_array );
由于您已经在每个td
中循环,因此您需要找到它所包含的tr
,然后查找选择
$("table tbody tr td.studentnumber").each(function (index) {
var grade = $(this).closest('tr').find('select.grade').val();
studentnum_array.push( {"studentnumber": $(this).text(), "grade": grade } );
});
console.log( studentnum_array );