I have the following 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行?

我try 过:

function getSelectedRowGuid() {
    var row = $("#MwDataList > input:radio[@name=selectRadioGroup]:checked :parent tr");
    var guid = GetRowGuid(row);
    return guid;
}

但这个 Select 器似乎不正确.

推荐答案

试试这个.

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() - Gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

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相关问答推荐

Sheets从多张sheet中导入range匹配数据

Flask,如何为ajax调用返回成功状态码

如何在 bootstrap 模式关闭并重新打开时重置 bootstrap 模式?

如何使用jQuery删除父元素

如何在 jQuery 中获取浏览器滚动位置?

以ajax方式在rails 3中提交表单(使用jQuery)

实用的 javascript 面向对象设计模式示例

按百分比zoom div的内容?

scrollIntoView 是否适用于所有浏览器?

jQuery.extend 和 jQuery.fn.extend 的区别?

jQuery Force 为 iframe 设置 src 属性

复选框值始终为打开

在jQuery中添加ID?

如何检测是否安装了 Flash,如果没有,显示一个隐藏的 div 通知用户?

jQuery Uncaught TypeError: 对象 [object Window] 的属性$不是函数

如何让 twitter bootstrap 子菜单在左侧打开?

使用 jQuery 比较两个 Javascript 对象数组

超时 jQuery 效果

如何删除集中的 contenteditable pre 周围的边框?

如何判断值是否为 JSON 对象?