繁体   English   中英

jQuery查询表行中多个表数据的每个获取值

[英]JQuery each getting value of multiple table data in a table row

嗨,我有这个表,其中有多行。 我想知道如何在此表的每一行中获取某些数据。 在此表中,我需要获取学生人数数据及其相应的成绩。

<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>

我试过的是在jquery中使用each循环,但我不知道如何使用它来获得类似/内联选择器。

 $("table tbody tr td.studentnumber").each(function (index) {
        studentnum_array.push( {"studentnumber": $(this).text(), "grade": $('table tbody tr td select.grade').val() } );
    });

     console.log( studentnum_array );

但是,在grade指数中,它只需要第一个。 它应该在每行中接受与td studentnumber类似的select值。

您可以遍历行而不是单元格...

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然后找到select

 $("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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM