jQuery: get parent tr for selected radio button

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

I have the following HTML:


    ....

    

Get All Table Cell

$tds = $row.find("td"); // Finds all children $tds = $row.find("td:nth-child(2)"); // Finds the 2nd
2条回答
  •  粉色の甜心
    2021-01-31 07:30

    Answer

    $("#MwDataList input[name=selectRadioGroup]:checked").closest('tr');
    

    How to find the closest row?

    Using .closest():

    var $row = $(this).closest("tr");
    

    Using .parent():

    Check this .parent() method. This is alternative of a .prev() and .next().

    var $row = $(this).parent()             // Moves up from 
.parent(); // Moves up from to
var $row = $(this).closest("tr"),       // Finds the closest row 
elements $.each($tds, function() { // Visits every single element console.log($(this).text()); // Prints out the text within the });

VIEW DEMO


Get Only Specific

var $row = $(this).closest("tr"),        // Finds the closest row 
element $.each($tds, function() { // Visits every single element console.log($(this).text()); // Prints out the text within the });

VIEW DEMO


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

提交回复
热议问题