2012-01-27 56 views
0

到複選框的數據表中的陣列我有一個表如下如何從一個jquery取第二式柱細胞受試者在第一列

<table id="vainTbl6" class="dtable" cellpadding="3" cellspacing="1" border="0"> 
    <thead> 
     <tr><th>check</td><th>Morphosal</th><th>goat </th><th>the other</th></tr> 
    </thead> 
    <tbody id="tbdy"> 
     <tr class="gradeX"> 
      <td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%">-Approve the femuneration Gommiqee purpirt</td> 
      <td>pontpose</td></tr> 
     <tr class="gradeX"> 
      <td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%">-Declare a final truce</td> 
      <td>More</td></tr> 
     <tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%">-Amend the articles of asscotonation</td> 
      <td>Four</td></tr> 
     <tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%">-Re-elect Bandanna la banana for warden</td> 
      <td>Floor</td></tr> 
    </tbody> 
</table> 

和第一列是一個複選框。我需要獲取第一列中的複選框設置爲true的所有第二列單元格

我試過用這個,但無濟於事。

function extractRowCell(divNode){ 
    alert(divNode.id); 
    $('#tbdy tr td').each(function() {  
    alert('hello'); 
    var aRowData = this.cells 
    alert(aRowData[1].firstChild.value); 
      return aRowData; 
    }); 
} 

調用如下:

<a id="la" href='#' onclick='extractRowCell(this.parentNode)' style="position:absolute; top:280px; left:350px;">Votes & Concerns</a> 

函數中的警報觸發雖然正確的值。

TIA

回答

1

遍歷td's只需做到以下幾點。

$('#tbdy tr td').each(function() {  
    alert($(this).text()); 
}); 
+0

你的代碼是不是爲我工作......這將是隻有一行?對?也許我應該首先執行$('#tbdy tr')。(函數(){alert($(this).childNodes [1] .text()); }); – DKean 2012-01-27 21:04:35

+0

這將是所有行和所有td的。是的,有很多方法可以做到這一點。用你的代碼,你可以做'$(this).find('td')。(函數(){alert($(this).text())})' – 2012-01-27 21:11:57

+0

哇,我必須道歉。它確實有效。出於某種原因,瀏覽器不會完全刷新,有時!我明白這是如何工作的。唯一的另一件事是確定何時複選框是'真'所以,感謝您的代碼。它工作正常! – DKean 2012-01-27 21:22:11

1

你可以做到這一點

<table><tbody id="tbdy"> 
<tr class="gradeX"> 
      <td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%" class="item">-Approve the femuneration Gommiqee purpirt</td> 
      <td>pontpose</td></tr> 
     <tr class="gradeX"> 
      <td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%" class="item">-Declare a final truce</td> 
      <td>More</td></tr> 
     <tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%" class="item">-Amend the articles of asscotonation</td> 
      <td>Four</td></tr> 
     <tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td> 
      <td style="width:55%" class="item">-Re-elect Bandanna la banana for warden</td> 
      <td>Floor</td></tr> 
</tbody> 
</table> 
<button>Get</button> 
<script> 
    $("button").click(function(){ 
    var ret=""; 
    $("#tbdy tr td:first").each(function() 
    { 
    if($("input:checked").length!=0) 
    {  
     ret=$("input:checked").parent().parent().find("td.item").text(); 

    }  
    }); 
    alert(ret); 
    }); 

</script> 
+0

謝謝。這解決了複選框。儘管我不能在那裏使用onclick事件,但會嘗試。如果用戶改變主意並取消點擊... – DKean 2012-01-27 23:37:33

相關問題