如何遍历表格行并使用 jQuery 获取单元格值 [英] How to iterate through a table rows and get the cell values using jQuery

查看:29
本文介绍了如何遍历表格行并使用 jQuery 获取单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 动态创建下表...执行我的代码后,我得到如下表格:

I am creating the below table dynamically using jQuery... After executing my code I get the table as below:

<table id="TableView" width="800" style="margin-left: 60px">
<tbody>
 <tr>
 <th>Module</th>
 <th>Message</th>
</tr>
<tr class="item">
 <td> car</td>
 <td>
  <input class="name" type="text">
 </td>
 <td>
<input class="id" type="hidden" value="5">
</td>
   </tr>
<tr class="item">
 <td> bus</td>
 <td>
  <input class="name" type="text">
 </td>
 <td>
<input class="id" type="hidden" value="9">
</td>
  </tr>

我曾经像这样迭代表:

 $("tr.item").each(function() {
            var quantity1 = $this.find("input.name").val();
        var quantity2 = $this.find("input.id").val();

            });

通过使用上面的查询,我只获取第一行单元格的值...帮助我使用 jQuery,它将遍历表的整行并获取 quantity1 中的行单元格值和quantity2.

By using the above query I am getting values of first row cells only... help me with jQuery that will iterate through the whole rows of the table and get the row cell values in quantity1 and quantity2.

推荐答案

$(this) 而不是 $this

$("tr.item").each(function() {
        var quantity1 = $(this).find("input.name").val(),
            quantity2 = $(this).find("input.id").val();
});

Proof_1:

proof_2:

这篇关于如何遍历表格行并使用 jQuery 获取单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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