jQuery:获取选定单选按钮的父 tr
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9314902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: get parent tr for selected radio button
提问by Andriy Khrystyanovich
I have the following HTML:
我有以下 HTML:
<table id="MwDataList" class="data" width="100%" cellspacing="10px">
....
<td class="centerText" style="height: 56px;">
<input id="selectRadioButton" type="radio" name="selectRadioGroup">
</td>
....
</table>
In other words I have a table with few rows, in each row in the last cell I have a radio button.
How can I get parentrow for selected radio button?
换句话说,我有一个只有几行的表格,在最后一个单元格的每一行中,我都有一个单选按钮。
如何获取选定单选按钮的父行?
What I have tried:
我尝试过的:
function getSelectedRowGuid() {
var row = $("#MwDataList > input:radio[@name=selectRadioGroup]:checked :parent tr");
var guid = GetRowGuid(row);
return guid;
}
But it seems like this selector is incorrect.
但似乎这个选择器是不正确的。
回答by ShankarSangoli
Try this.
尝试这个。
You don't need to prefix attribute name by @
in jQuery selector. Use closest()
method to get the closest parent element matching the selector.
您不需要@
在 jQuery 选择器中为属性名称添加前缀。使用closest()
方法获取与选择器匹配的最近父元素。
$("#MwDataList input[name=selectRadioGroup]:checked").closest('tr');
You can simplify your method like this
你可以像这样简化你的方法
function getSelectedRowGuid() {
return GetRowGuid(
$("#MwDataList > input:radio[@name=selectRadioGroup]:checked :parent tr"));
}
closest()
- Gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
closest()
- 获取与选择器匹配的第一个元素,从当前元素开始并向上遍历 DOM 树。
As a side note, the ids of the elements should be unique on the page so try to avoid having same ids for radio buttons which I can see in your markup. If you are not going to use the ids then just remove it from the markup.
作为旁注,元素的 id 在页面上应该是唯一的,所以尽量避免在你的标记中看到的单选按钮具有相同的 id。如果您不打算使用 ID,则只需将其从标记中删除即可。
回答by Jay Patel
Answer
回答
$("#MwDataList input[name=selectRadioGroup]:checked").closest('tr');
How to find the closest row?
如何找到最近的行?
Using .closest()
:
使用.closest()
:
var $row = $(this).closest("tr");
Using .parent()
:
使用.parent()
:
Check this .parent()
method. This is alternative of a .prev()
and .next()
.
检查这个.parent()
方法。这是 a.prev()
和 的替代方案.next()
。
var $row = $(this).parent() // Moves up from <button> to <td>
.parent(); // Moves up from <td> to <tr>
Get All Table Cell <td>
获取所有表格单元格 <td>
var $row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td"); // Finds all children <td> elements
$.each($tds, function() { // Visits every single <td> element
console.log($(this).text()); // Prints out the text within the <td>
});
Get Only Specific <td>
只获取特定的 <td>
var $row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td:nth-child(2)"); // Finds the 2nd <td> element
$.each($tds, function() { // Visits every single <td> element
console.log($(this).text()); // Prints out the text within the <td>
});
Useful methods
有用的方法
.closest()
- get the first element that matches the selector.parent()
- get the parent of each element in the current set of matched elements.parents()
- get the ancestors of each element in the current set of matched elements.children()
- get the children of each element in the set of matched elements.siblings()
- get the siblings of each element in the set of matched elements.find()
- get the descendants of each element in the current set of matched elements.next()
- get the immediately following sibling of each element in the set of matched elements.prev()
- get the immediately preceding sibling of each element in the set of matched elements
.closest()
- 获取与选择器匹配的第一个元素.parent()
- 获取当前匹配元素集合中每个元素的父元素.parents()
- 获取当前匹配元素集合中每个元素的祖先.children()
- 获取匹配元素集中每个元素的子元素.siblings()
- 获取匹配元素集中每个元素的兄弟.find()
- 获取当前匹配元素集中每个元素的后代.next()
- 获取匹配元素集中每个元素的紧随其后的兄弟元素.prev()
- 获取匹配元素集中每个元素的前一个兄弟元素