为什么我得到整排?
我的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Color</th>
<th>Size</th>
<th>Position</th>
<th>Print</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dummy1.1</td>
<td>Dummy1.2</td>
<td>Dummy1.3</td>
<td>Dummy1.4</td>
<td><button>Print</button></td>
</tr>
<tr>
<td>Dummy2.1</td>
<td>Dummy2.2</td>
<td>Dummy2.3</td>
<td>Dummy2.4</td>
<td><button>Print</button></td>
</tr>
</tbody>
</table>
<script>
$(document).ready(function(){
$('table tbody tr td:last-child').on('click', function(){
var $row = $(this).parent('tr:not(:last-child)').text();
console.log($row);
});
});
</script>
</body>
</html>
当我点击按钮时,我想打印除打印表数据之外的行。 我的代码不能正常工作,因为它显示了整行。
感谢您的帮助
答案 0 :(得分:0)
您正在检查tr
是不是最后一个孩子并且正在尝试使用该选择器查找父级。它应该是
$('table tbody tr td:last-child').on('click', function() {
var $row = $(this).parent().find('td:not(:last-child)').text();
console.log($row);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Name</th>
<th>Color</th>
<th>Size</th>
<th>Position</th>
<th>Print</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dummy1.1</td>
<td>Dummy1.2</td>
<td>Dummy1.3</td>
<td>Dummy1.4</td>
<td>
<button>Print</button>
</td>
</tr>
<tr>
<td>Dummy2.1</td>
<td>Dummy2.2</td>
<td>Dummy2.3</td>
<td>Dummy2.4</td>
<td>
<button>Print</button>
</td>
</tr>
</tbody>
</table>