繁体   English   中英

如何通过jQuery获取表的行ID

[英]how to get row's id of table through jquery

这是我的表格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获取所有三行的ID(即141,144,147)。 我尝试过.find .closest,但uptill现在还没有成功。

您可以使用Has-Attribute选择器

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

这将选择具有id属性的tr

小提琴

尝试:

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/

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

尝试这个

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

这是上面代码的小提琴

暂无
暂无

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

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