2014-04-23 37 views
0

我有類似JavaScript來提取的兄弟姐妹,然後將數據發送到Django的

<tbody id="order_table"> 
     <tr class="list"> 
      <td data-status="old" class="food_name">food1</td> 
      <td class="quantity">1</td> 
      <td class="price">$10.00</td> 
      <td><button type="button" class="btnIncrement btn"> <span class="fa fa-plus"></span></button></td> 
     </tr> 
     <tr class="list"> 
      <td data-status="old" class="food_name">food111</td> 
      <td class="quantity">1</td> 
      <td class="price">$10.00</td> 
      <td><button type="button" class="btnIncrement btn"> <span class="fa fa-plus"></span></button></td> 
     </tr> 
     <tr class="list"> 
      <td data-status="new" class="food_name">food11</td> 
      <td class="quantity">1</td> 
      <td class="price">$10.00</td> 
      <td><button type="button" class="btnIncrement btn"> <span class="fa fa-plus"></span></button></td> 
     </tr> 
     <tr class="list"> 
      <td data-status="new" class="food_name">food12</td> 
      <td class="quantity">1</td> 
      <td class="price">$10.00</td> 
      <td><button type="button" class="btnIncrement btn"> <span class="fa fa-plus"></span></button></td> 
     </tr> 
</tbody> 

我想提取每個new項的食品名稱,數量及價格,並付諸陣列,最後發送到Django的由ajax .. 訂單並不重要 這就是我想要的。

[[food11, 1, 10.00], [food12, 1, 10.00]] 

如果沒有新的項目存在,就會返回

[] 

我真的不能看着辦吧。所以我想一些幫助。非常感謝你

回答

1
var newFood = []; 

$('#order_table td.food_name').filter(function() { 
    return $(this).data('status') == 'new'; 
}).each(function() { 
    newFood.push([$(this).text(), $(this).siblings('.quantity').text(), $(this).siblings('.price').text()]); 
}); 

$.ajax({ 
    // do you ajax 
});