I need some help with selecting a cell in my gridview. I have a table like this:
<table>
<tr>
<th>CheckBox</th>
<th>Customer ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td>CheckBox</td>
<td>1</td>
<td>Joe</td>
<td>Blogs</td>
</tr>
<tr>
<td>CheckBox</td>
<td>2</td>
<td>Chris</td>
<td>W开发者_开发技巧hite</td>
</tr>
</table>
I need to select the ID cell of the row that is currently checked. How would you do this?
I have had a search but cant seem to find anything like the above.
$(":checkbox").click(function(){
if(this.checked){
var id = $(this).parent().next().text();
// assuming your second column has id you're looking for [customer id]
}
});
wokring demo
In theory, this would work:
$('input:checkbox').change(
function(){
if ($(this).is(':checked')) {
var theRowId = $(this).closest('tr').attr('id');
}
});
A quick and dirty JS Fiddle demo.
Edited: to atone for my misunderstanding the question, and the html:
Given that the number you want to find is stored within a cell (a cell to which I've assigned a class 'rowID', for ease of access) the following works:
$(document).ready(
function() {
$('.rowID').each(
function(i){
$(this).text(i+1);
});
$('input:checkbox').change(
function() {
if ($(this).is(':checked')) {
var theRowId = $(this).parent().siblings('.rowID').text();
$('#rowId').text(theRowId);
}
});
});
JS Fiddle demo
Well, your base structure is:
<tr>
<td>CheckBox</td>
<td>2</td>
<td>Chris</td>
<td>White</td>
</tr>
So this may solve your problem:
$(document).ready(function()
{
$('tr td').find('checkbox').click(function()
{
var line_id = $(this).parent('td').next().text();
});
});
I hope it helps! ^^
精彩评论