2017-10-11 39 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

他们不工作兄弟 –

相关问题