使用.val()从表中获取最后一行数据,并使用jquery将其显示在警报中,可以按照以下步骤进行操作:
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<table id="myTable">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
</tr>
</table>
var lastRow = $('#myTable tr:last');
var name = lastRow.find('td:first').text();
var age = lastRow.find('td:last').text();
<div id="alertContainer"></div>
var alertMessage = 'Name: ' + name + ', Age: ' + age;
$('#alertContainer').html('<div class="alert alert-success">' + alertMessage + '</div>');
完整的示例代码如下:
<!DOCTYPE html>
<html>
<head>
<title>Get Last Row Data using .val()</title>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<table id="myTable">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
</tr>
<tr>
<td>Mike</td>
<td>35</td>
</tr>
</table>
<div id="alertContainer"></div>
<script>
$(document).ready(function() {
var lastRow = $('#myTable tr:last');
var name = lastRow.find('td:first').text();
var age = lastRow.find('td:last').text();
var alertMessage = 'Name: ' + name + ', Age: ' + age;
$('#alertContainer').html('<div class="alert alert-success">' + alertMessage + '</div>');
});
</script>
</body>
</html>
这样,最后一行的数据将会显示在警报中。请注意,这只是一个示例,你可以根据实际需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云