2009-10-20 69 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> 

编辑:添加标签警报()

相关问题