2009-10-20 71 views
1

我有一個包含5個單元格的表格行。使用JQuery從表格行中的控件中選擇文本

我想單擊最後一個單元格中的圖像,並從第一個單元格中的單選按鈕中獲取文本值。

有人可以告訴我我需要的jQuery命令嗎?

目前,我正在做它喜歡:

// when the image is clicked 

alert($(this)[0].parentElement.parentElement.children[0].childNodes[1].innerText); 

但直覺告訴我這是一個有點太冗長是正確的。

有什麼想法?

感謝

戴夫

+0

你能提供的標記?有些東西告訴我,*不是*跨瀏覽器一致,'innerText'是IE特定的,我認爲'children'也是。 – 2009-10-20 16:12:14

回答

1

標記將是一件好事知道這裏,單選按鈕:有不止一個?如果需要,只需要選擇一個?您可以使用類似的選擇: 檢查一個:

$(this).parent().sibling('td').children('input:radio:checked').val(); 

第一列中選​​中一個:

$(this).parent().sibling('td:first').children('input:radio:checked').val(); 
0

也許是這樣的:

$(this).closest('tr').find('td:first input[type=radio]').val(); 

這是沒有你的標記有點艱難,但應該是一個良好的開端。

0

帶標記的示例。 (使用按鈕而不是IMG)

<table border="1"> 
    <tr> 
     <td><input type="radio" name="example" value="foo" /> Foo Label</td> 
     <td>stuff</td> 
     <td><button onclick="checkIt(this)">Check it</button></td> 
    </tr> 
</table> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> 

<script type="text/javascript"> 

    function checkIt(button){ 
     // Alert the value of the radio 
     alert($(button).parents('tr').find('td:first :radio').val()); 

     // Alert the label of the radio 
     alert($(button).parents('tr').find('td:first').contents().filter(function() { 
      return this.nodeType == Node.TEXT_NODE; 
     })[0].data) 
    }; 
</script> 

編輯:添加標籤警報()

相關問題