【问题标题】:Jquery Retrieve cell value based on state of another cellJquery根据另一个单元格的状态检索单元格值
【发布时间】:2011-05-22 05:06:30
【问题描述】:

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

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

【问题讨论】:

  • 您的 tr 元素没有 id 属性。请张贴真正的标记。
  • @David Thomas - 我相信“id cell”是每一行的第二列。

标签: jquery html-table css-selectors cells


【解决方案1】:

理论上,这是可行的:

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

A quick and dirty JS Fiddle demo.


已编辑: 以弥补我对问题的误解,以及 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 Fiddle demo

【讨论】:

    【解决方案2】:

    嗯,你的基本结构是:

    <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();
        });
    });
    

    希望对你有帮助! ^^

    【讨论】:

      【解决方案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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 1970-01-01
        • 2019-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多