获取< td>值仅在使用jQuery的特定行中
问题描述:
我有一个表(id ="docsTable"),其行看起来与此类似:
I have a table (id="docsTable") whose rows look similar to this:
<tr bgcolor="#E7DDCC">
<td align="center"><input type="checkbox" name="docsToAddToClass[]" value="35" /></td>
<td>Document Title</td>
<td>Document Description.</td>
</tr>
我需要遍历表,确定用户已经选中了哪些复选框,对于带有选中复选框的行,获取第一个值和接下来两个的文本.
I need to iterate through the table, determine which checkboxes the user has checked, and for the rows with a checked checkbox, grab the value for the first and the text for the next two.
我不需要构建集合:在每次迭代中,我想更改其他地方的某些元素. (对我而言)这不是困难的部分.它试图找出如何遍历表格并仅选中带有选中复选框的.
I don't need to build a collection: Within each iteration I want to change some elements elsewhere. That's not the hard part (for me). It's trying to figure out how to iterate through the table and select only the s with checked checkboxes.
答
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
<script type="text/javascript">
$(function(){
$('#btnTest').click(function(){
$('#docsTable input[type="checkbox"]:checked').each(function(){
var $row = $(this).parents('tr');
alert($row.find('td:eq(0) input').val());
alert($row.find('td:eq(1)').html());
alert($row.find('td:eq(2)').html());
});
});
});
</script>
</head>
<body>
<table id="docsTable">
<tr bgcolor="#E7DDCC">
<td align="center"><input type="checkbox" name="docsToAddToClass[]" value="35" /></td>
<td>Document Title 1</td>
<td>Document Description.</td>
</tr>
<tr bgcolor="#E7DDCC">
<td align="center"><input type="checkbox" name="docsToAddToClass[]" value="36" /></td>
<td>Document Title 2</td>
<td>Document Description.</td>
</tr>
<tr bgcolor="#E7DDCC">
<td align="center"><input type="checkbox" name="docsToAddToClass[]" value="37" /></td>
<td>Document Title 3</td>
<td>Document Description.</td>
</tr>
</table>
<input type='button' id='btnTest' value='Get Rows' />
</body>
</html>