[英]Select Text from Control in Table Row with JQuery
I have a table row with 5 cells. 我有一个包含5个单元格的表格行。
I want to click an image in the last cell, and get the text value from a radio button in the first cell. 我想单击最后一个单元格中的图像,并从第一个单元格中的单选按钮获取文本值。
Can somebody tell me jQuery command I need? 有人可以告诉我我需要的jQuery命令吗?
I'm currently doing it like : 我目前正在这样做:
// when the image is clicked
alert($(this)[0].parentElement.parentElement.children[0].childNodes[1].innerText);
but something tells me this is a bit too verbose to be correct. 但是有件事告诉我,这太冗长了以至于不正确。
any ideas? 有任何想法吗?
Thanks 谢谢
Dave 戴夫
Markup would be good to know here, radio button: are there more than one? 最好在这里知道标记,单选按钮:是否有多个? Just need the selected one if so?
是否需要选择的一个? You might use the selectors like: checked one:
您可能会使用以下选择器:选中一项:
$(this).parent().sibling('td').children('input:radio:checked').val();
first column checked one: 第一列检查一个:
$(this).parent().sibling('td:first').children('input:radio:checked').val();
Maybe something like: 也许像这样:
$(this).closest('tr').find('td:first input[type=radio]').val();
It's a little tough without your markup, but that should be a good start. 没有标记,这有点困难,但这应该是一个好的开始。
Example with markup. 标记示例。 (using button instead of img)
(使用按钮代替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>
Edit: Added label alert() 编辑:添加了标签alert()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.