jQuery:为选定的单选按钮获取父tr [英] jQuery: get parent tr for selected radio button

查看:133
本文介绍了jQuery:为选定的单选按钮获取父tr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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>

换句话说,我有一个只有几行的表,在最后一个单元格的每一行中,我都有一个单选按钮.
如何获得所选单选按钮的 parent 行?

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 parent row for selected radio button?

我尝试过的事情:

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.

推荐答案

尝试一下.

您无需在jQuery选择器中的@前面加上属性名称.使用closest()方法获取与选择器匹配的最接近的父元素.

You don't need to prefix attribute name by @ in jQuery selector. Use closest() method to get the closest parent element matching the selector.

$("#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()-获取与选择器匹配的第一个元素,从当前元素开始,一直扩展到DOM树.

closest() - Gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

请注意,元素的ID在页面上应该是唯一的,因此请尽量避免在标记中看到相同的单选按钮ID.如果您不打算使用ID,则只需将其从标记中删除即可.

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.

这篇关于jQuery:为选定的单选按钮获取父tr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆