2017-10-11 48 views
0

我有一個表像下面如何使用JQuery獲取記錄表並使用AJAX將其發送到服務器?

<table cellspacing="0" rules="all" border="1" id="ContentPlaceHolder1_GridView1" style="border-collapse:collapse;" class="table table-striped"> 
    <tbody> 
     <tr> 
      <th scope="col">&nbsp;</th> 
      <th scope="col">Bill NO</th> 
      <th scope="col">Date</th> 
      <th scope="col">Description</th> 
      <th scope="col">Type</th> 
      <th scope="col">Amount</th> 
     </tr> 
     <tr> 
      <td><input id="Checkbox0" class="checkBoxClass" type="checkbox">/td> 
      <td>111</td> 
      <td>12-22-2014</td> 
      <td>computer problem</td> 
      <td>Invoice</td> 
      <td class="ActualAmount">7689.00</td> 
     </tr> 
     <tr> 
      <td><input id="Checkbox1" class="checkBoxClass" type="checkbox">/td> 
      <td>112</td> 
      <td>12-22-2014</td> 
      <td>Printer problem</td> 
      <td>Invoice</td> 
      <td class="ActualAmount">7689.00</td> 
     </tr> 
    </tbody> 
</table> 

我想獲得的BillNoActualAmount的值,如果在該行

首先選中複選框,我嘗試使用,以獲取該行的值這下面的代碼

alert('value = ' + $("#ContentPlaceHolder1_GridView1 .checkBoxClass input[type='checkbox']:checked").closest("td").val()) 

它提醒value = undefined

我試圖得到這樣Jfiddle

+0

那個小提琴與你的代碼有什麼關係?注意:''最接近(「td」)'輸入是封閉輸入的'td'。你可能想要它是下一個兄弟而不是 –

+0

@Jaromandax不,我只是試過它是否顯示下一個值。但我只想得到第2和第6個td值。每行有10個td,爲了清楚起見,我在這裏刪除了一些td元素 –

+0

,但是最接近輸入的是td將輸入括起來,這樣你發佈的代碼是完全不足的 –

回答

1

如果你想獲得的是有一個複選框選中所有行,並保存在對象的數組值...

var values = []; 

$('table#ContentPlaceHolder1_GridView1 input.checkBoxClass:checked').each(function() { 
    var $row = $(this).closest('tr').children('td'); 
    values.push({ 'billno': $row.eq(1).text(), 'amount': $row.eq(5).text() }); 
}); 

我希望它能幫助

+0

@ A.lglesias請稍候,我會更新 –

+0

@ A.lglesias我希望你的代碼是正確的,但是當你的代碼 –

+0

'value'是一個對象數組後,'alert(values)'時它不會提醒任何事情。爲了提醒它的價值,你可以做'alert(JSON.stringify(values));' –

0

值試試這個在您的警報消息

$("#ContentPlaceHolder1_GridView1 input[type='checkbox'].checkBoxClass:checked").parent().next().html() 
0

喜歡的東西

$("#ContentPlaceHolder1_GridView1 .checkBoxClass input[type='checkbox']:checked").closest("tr").find("td:eq(1)").text() 

將爲BillNo工作(但應該會更好在你的html中爲這一列添加一個類或數據標籤,就像你爲actualAmount所做的那樣)。

對於實際金額:

$("#ContentPlaceHolder1_GridView1 .checkBoxClass input[type='checkbox']:checked").closest("tr").find("td.ActualAmount").text() 
+0

他們不工作兄弟 –

相關問題