jQuery查询表行中多个表数据的每个获取值 [英] JQuery each getting value of multiple table data in a table row

查看:109
本文介绍了jQuery查询表行中多个表数据的每个获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有这张表格,其中有多行. 我想知道如何在此表的每一行中获取某些数据.在此表中,我需要获取学生人数数据及其伴随的成绩.

Hi there I have this table which has multiple rows. I want to know how to get certain data in each row of this table. In this table I needed to get the student number data and it's accompanying grade.

<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循环,但是我不知道如何使用它来获得相似/内联选择器.

What I have tried is using the each loop in jquery but I don't know how get similar/inline selectors with it.

 $("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值.

But in the grade index it only takes the first one. It should take in the value of the select in each row similar to the td studentnumber.

推荐答案

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

You can loop through the rows instead of the cells...

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

If you want to loop through the cells, you have to find the select in relation to the corresponding studentnumber cell...

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

这篇关于jQuery查询表行中多个表数据的每个获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆