2017-01-13 9 views
0

私はこれが私のテーブルコードで 、すべてのTR IDを取得し、jQueryの配列にそれを登録したい:<tr> IDをテーブルに登録しますか?

<div class="table-responsive" style="margin-top: 10px"> 
    <table class="table table-striped table-bordered" id="tabletmpitem"> 
     <thead> 
     <tr> 
      <th>EAN</th> 
      <th>Item Name</th> 
      <th>Old Price</th> 
      <th>New Price</th> 
     </tr> 
     </thead> 
     <tbody id="tbodytmpitem"> 
      <tr id="1"><td></td> 
      <tr id="2"><td></td> 
     </tbody> 
    </table> 
</div> 

そのすべてのIDを取得し、jQueryの配列に割り当てる方法は? テーブル行にどのような値が存在するかをチェックするために使用しますか? 私が望むのは、すべてのtr idを取得してjQuery配列に割り当てることです。

答えて

5

のtbodyでTRを反復し、アレイ

var arr = []; 
 

 
$("#tbodytmpitem tr").each(function() { 
 
    arr.push(this.id); 
 
}); 
 

 
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="table-responsive" style="margin-top: 10px"> 
 
    <table class="table table-striped table-bordered" id="tabletmpitem"> 
 
    <thead> 
 
     <tr> 
 
     <th>EAN</th> 
 
     <th>Item Name</th> 
 
     <th>Old Price</th> 
 
     <th>New Price</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody id="tbodytmpitem"> 
 
     <tr id="1"> 
 
     <td></td> 
 
     <tr id="2"> 
 
      <td></td> 
 
    </tbody> 
 
    </table> 
 
</div>

+1

@yoseにそれをプッシュする:それは助けた場合の答えとしてマークしてください。 –

+0

はい私はそれをマークするために10分待つ必要があります – yose

2

.map()を使用して、すべてtrを反復してIDを返します。次に、$.makeArray()を使用して結果を配列に変換します。

var array = $.makeArray($('tbody tr[id]').map(function() { 
 
    return this.id; 
 
})); 
 
console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="table-responsive" style="margin-top: 10px"> 
 
    <table class="table table-striped table-bordered" id="tabletmpitem"> 
 
    <thead> 
 
     <tr> 
 
     <th>EAN</th> 
 
     <th>Item Name</th> 
 
     <th>Old Price</th> 
 
     <th>New Price</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody id="tbodytmpitem"> 
 
     <tr id="1"> 
 
     <td></td> 
 
     <tr id="2"> 
 
      <td></td> 
 
    </tbody> 
 
    </table> 
 
</div>

関連する問題