如何通过jquery获取表的行ID

时间:2014-04-18 10:18:10

标签: javascript jquery html

这是我的表格HTML

<table id="list" class="ui-jqgrid-btable" border="0" cellspacing="0" cellpadding="0" tabindex="0" role="grid" aria-multiselectable="false" aria-labelledby="gbox_list" style="width: 1702px;">
 <tbody>
 <tr class="jqgfirstrow" style="height:auto" role="row">
 <tr id="141" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row">
 <tr id="144" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row">
 <tr id="147" class="ui-widget-content jqgrow ui-row-ltr ui-state-highlight"    tabindex="0" role="row" aria-selected="true">
</tbody>
 </table>

我想使用jquery获取所有三行(即141,144,147)的ID。我试过.find .closest但是现在还没有取得任何成功。

4 个答案:

答案 0 :(得分:1)

您可以使用Has-Attribute选择器

$("#list tr[id]").each(function(){
   // Using alert just to show the result
   alert(this.id);
});

这将选择tr具有id属性

的{{1}}

Fiddle

答案 1 :(得分:0)

尝试:

var ids = $('#list').find('tr').map(function(){ return $(this).attr('id'); });

或:

var ids = $('#list').find('tr[id]').map(function(){ return this.id; });

输出:

[141, 144, 147]

http://jsfiddle.net/KbEyk/

答案 2 :(得分:0)

$("#list tr").each(function(obj){
 alert(obj.id);
});

答案 3 :(得分:0)

试试这个

$(document).ready( function() {
  var idArr = [];
  $("tr").each(function(){
    if($(this).attr("id")){
      idArr.push($(this).attr("id"));
    }      
  });
  console.log(idArr)
});

以上代码的a fiddle