1

在我的 gridview 中选择一个单元格时,我需要一些帮助。我有一张这样的桌子:

<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 单元格。你会怎么做?

我进行了搜索,但似乎找不到类似上述内容的内容。

4

3 回答 3

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

于 2010-12-08T12:44:59.647 回答
2

理论上,这会起作用:

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

一个快速而肮脏的 JS Fiddle 演示


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

鉴于您要查找的号码存储在一个单元格中(为便于访问,我已为其分配了一个类“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 小提琴演示

于 2010-12-08T12:44:06.963 回答
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();
    });
});

我希望它有帮助!^^

于 2010-12-08T12:47:56.310 回答