单击按钮时如何获取与类最接近的值?

时间:2019-03-10 03:37:44

标签: jquery

我有带有数据和按钮的表行;每个按钮对应每一行,当您单击一个按钮时,我也想获得与类最接近的<td>的值。
我使用下面的代码获取值,但是它始终显示空值,我想知道如何编辑记录,请问有人可以帮忙吗?

表结构:

<table id="myTable" class="table table-striped">
     <thead>
         <tr>
             <th>Student ID</th>
             <th>Student Name</th>
             <th>Email</th>
             <th>Department</th>
             <th>Action(Edit)</th>
             <th>Action(Delete)</th>
         </tr>
     </thead>
     <tbody id="SetStudentList">
         <tr id="LoadingStatus" style="color:red"></tr>
     </tbody>
</table>

<script>
  var SetData;
  $(document).ready(function () {

     var StudentList = [
         {
             "StudentId": 1,
             "StudentName": "ram",
             "Email": "ram@gmail.com",
             "isDeleted": "yes",
             "Dept": "dept"
         },
         {
             "StudentId": 2,
             "StudentName": "sam",
             "Email": "sam@gmail.com",
             "isDeleted": "no",
             "Dept": "business"
         },
         {
             "StudentId": 3,
             "StudentName": "ramadevi",
             "Email": "ramadevi@gmail.com",
             "isDeleted": "yes",
             "Dept": "mba"
         },
         {
             "StudentId": 4,
             "StudentName": "pooja",
             "Email": "pooja@gmail.com",
             "isDeleted": "no",
             "Dept": "inter"
         }
     ]

     $("#LoadingStatus").html("Loading....");
     SetData = $("#SetStudentList");
     for (var i = 0; i < StudentList.length; i++) {
       var Data = "<tr class='row_" + StudentList[i].StudentId + "'>" +
         "<td class='minAmt'>" + StudentList[i].StudentId + "</td>" +
         "<td class='minAmt'>" + StudentList[i].StudentName + "</td>" +
         "<td class='minAmt'>" + StudentList[i].Email + "</td>" +
         "<td class='minAmt'>" + StudentList[i].Dept + "</td>" +
         "<td class='minAmt'>" + "<a href='#' class='btn btn-warning' onclick='EditStudentRecord(" + StudentList[i].StudentId + ")' ><span class='glyphicon glyphicon-edit'></span></a>" + "</td>" +
         "<td class='minAmt'>" + "<a href='#' class='btn btn-danger' onclick='DeleteStudentRecord(" + StudentList[i].StudentId + ")'><span class='glyphicon glyphicon-trash'></span></a>" + "</td>" +
         "</tr>";
       SetData.append(Data);
       $("#LoadingStatus").html("");
     }
  });



  //Show The Popup Modal For Edit Student Record
  function EditStudentRecord(StudentId) {
      console.log("====>" + $(this).closest('tr').find('.minAmt').text());
  }

</script>

1 个答案:

答案 0 :(得分:0)

您可能需要使用.prev()方法来获取最接近的元素

$(this).closest('td').prev('.minAmt').text()

请参见以下工作示例,以动态编辑行a link