jQuery: get parent tr for selected radio button

前端 未结 2 1717
半阙折子戏
半阙折子戏 2021-01-31 06:59

I have the following HTML:


    ....

    
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 07:51

    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.

    $("#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.

提交回复
热议问题