2010-12-08 108 views
1

我需要一些帮助,在我的gridview中选择一个单元格。我有这样的表格:jquery根据另一个单元格的状态检索单元格值

<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>White</td> 
</tr> 
</table> 

我需要选择当前选中行的ID单元格。你会如何做到这一点?

我有过搜索,但似乎无法找到类似上述的东西。

+0

您的`tr`元素没有`id`属性。请发布真正的加价。 – 2010-12-08 12:42:33

回答

3
$(":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

2

从理论上讲,这会工作:

$('input:checkbox').change(
    function(){ 
     if ($(this).is(':checked')) { 
      var theRowId = $(this).closest('tr').attr('id'); 
     } 
    }); 

A quick and dirty JS Fiddle demo


编辑:,以弥补我的误解的问题,而html:

既然你想找到存储单元(一个单元内的号码我已经指派一个class 'ROWID',以便于访问)以下工作:

$(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

0

那么,你的基本结构是:

<tr> 
<td>CheckBox</td> 
<td>2</td> 
<td>Chris</td> 
<td>White</td> 
</tr> 

因此,这可能会解决你的问题:

$(document).ready(function() 
{ 
    $('tr td').find('checkbox').click(function() 
    { 
     var line_id = $(this).parent('td').next().text(); 
    }); 
}); 

我希望它能帮助! ^^

相关问题