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>
解决方案
我认为问题是:
$(this)
函数内部无法捕获所需的元素- 您
.minAmt
在该行中有更多具有此类的元素..因此您可以使用:eq()
它们中的哪一个来选择
//Show The Popup Modal For Edit Student Record
function EditStudentRecord(StudentId , el) {
------^^-----
console.log("====>" + $(el).closest('tr').find('.minAmt:eq(0)').text());
-----^^----- ----^^^^^----
}
和
onclick='EditStudentRecord(" + StudentList[i].StudentId + " , this)'
注意:如果上面的代码对您有用..那么您还需要对DELETE
函数进行相同的更改
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"
}
]
var SetData = $('#SetStudentList'); // this is a table selector
$("#LoadingStatus").html(""); // empty the loading status
// loop through the List using `.each()`jquery function
$.each(StudentList , function(key , value){
var Data = "<tr class='row_" + value.StudentId + "'>" +
"<td class='minAmt'>" + value.StudentId + "</td>" +
"<td class='minAmt'>" + value.StudentName + "</td>" +
"<td class='minAmt'>" + value.Email + "</td>" +
"<td class='minAmt'>" + value.Dept + "</td>" +
"<td class='minAmt control'>" + "<a href='#' class='btn btn-warning EditStudent'><span class='glyphicon glyphicon-edit'></span></a></td>" +
"<td class='minAmt control'>" + "<a href='#' class='btn btn-danger DeleteStudent'><span class='glyphicon glyphicon-trash'></span></a></td>" +
"</tr>";
SetData.append(Data); // append the data to the table
});
// I removed the `onclick` attribute we will use here the `click` jusery function
// I added EditStudent class to the edit span icon
$(document).on('click' , '.EditStudent' , function(){
var $this = $(this);
// get closest tr and loop through all tds .. and replace the html of the td with the input
// if you wonder what is `:not(.control)` this is because if we change the tds content that will affect the edit td and the delete td so to avoid this I set the class `control` to the `td`s which holds the edit and delete icons
$this.closest('tr').find('td:not(.control)').each(function(){
var getText = $(this).text(); // get the td text
$(this).html('<input type="text" value="'+getText+'"/>'); // replace the td html with input with the same text value
});
});
// delete click
$(document).on('click' , '.DeleteStudent' , function(){
// remove the closest row
$(this).closest('tr').remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<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>
推荐阅读
- java - 如何在 SpringMVC 中使用单个视图执行所有 CRUD 操作?
- python - VS Code 和 python 调试器返回错误但可以在终端中运行
- c# - QTP/UFT 测试:可等待的 WPF 应用程序(.net)
- c# - MVC Net Core 与 Docker 容器问题
- javascript - Nashorn:猴子修补 Java 对象?
- excel - 如何遍历各个单元格并在另一个工作表中返回值
- android - 如何传递和检索二维数组作为意图
- c++ - 如何获取资源文件中对话框的按钮控件的大小和位置?
- laravel - 如何为 Laravel 资源 API 进行分页
- c# - 如何将下载的文件保存到本地文件夹?