当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery获取Table某列的值(推荐)

jQuery获取Table某列的值(推荐)

2019年06月13日  | 移动技术网IT编程  | 我要评论

在写此篇博文时,发现在以前曾写过《获取datatable选择第一行某一列值》 。

但是与此篇所说的完全不一样。这篇insus.net需要的是jquery去获取html table的某一行某一列的数据。

如下表:

html code:

<table>
    <tr>
      <th style="width:10px;"><input id="selectall" type="checkbox" /></th>
      <th>id</th>
      <th>费用名目</th>
      <th>费用解释</th>
      <th>收费明细</th>
      <th>币种</th>
      <th style="width:50px;">操作</th>
    </tr>
    @foreach (var m in new highwayadditionalchargeentity().highwayadditionalcharges())
      {
      <tr class="trdata">
        <td><input id="" class="selectsingle" type="checkbox" value="@m.highwayadditionalcharge_nbr" /></td>
        <td>@m.highwayadditionalcharge_nbr</td>
        <td>@m.item</td>
        <td>@m.description</td>
        <td>@m.itemizations</td>
        <td>@m.currency</td>
        <td>
          <input class="select" id="buttonselect" type="button" value="选择" />
        </td>
      </tr>
    }
  </table>

 当用户点击某一行最后一列的“选择”铵钮时,想获取此铵钮本行中某一列的数据。

insus.net有在图中示出,列与索引。索引是从0开始。如想获取“费用名目”列值,此列的列索引是2。

 演示:

上面我们获取值,是使用了.text()方法。但某一时候,你想获取列的值,它是html代码,那我们可以使用.html()方法。下面insus.net稍修改一下:

演示:

其实,使用索引来获取值,只是一种方法,但它不是insus.net最理想的方法首选。由于数据行是动态呈现,列也有可能会变更。因此insus.net还是习惯使用样式class来实现:

举个列子,想获取“费用解释”列的值,在此列添加一个class:


jquery代码:

演示:

以上所述是小编给大家介绍的jquery获取table某列的值,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网