使用jquery获取动态表td值

时间:2017-10-24 09:15:11

标签: jquery codeigniter

我的代码

<table id="datatable" class="table table-bordered table-striped">
<thead>
    <tr  class="md-bg-blue-grey-200">
        <th width="11%">Sl. No.</th>
        <th>State Name</th>            
        <th>District Name</th>            
    </tr>    
    </thead>
<tbody>
<?php foreach ($district_name as $district_names){?>
    <tr>            
        <td><input type="radio" name="district_id" value="<?php echo $district_names['district_id']?>"  style="margin-left: 10px" checked>
        </td>
        <td><?php echo $district_names['state']?></td>
        <td id="district_name"><?php echo $district_names['district']?></td>

    </tr><?php }?>

</tbody>
</table>
   <div style="margin: 0 auto; width: 100px;">
  <a href="javascript:Setvalues()" class="btn btn-primary btn-sm pull-right"  
          style="cursor:pointer;" onclick="selectDistrict()">Select</a>
</div>
</div>

当我选择此函数调用的行时,这是我的函数。

<script>            
        function selectDistrict() {
            district_id = $('input[name=district_id]:checked').val();
            district_name 
            =$('#datatable').find("#district_name").eq(2).html();                             
            opener.district_select(district_id,district_name);              
            window.close();
        }
    </script>

我想获取分区名称列值,但它显示未定义,如何从此表中获取分区名称。

1 个答案:

答案 0 :(得分:0)

尝试以下代码

district_name = $('input[name=district_id]:checked').parent().parent().find("td:eq(1)").text();

而不是

district_name = $('#datatable').find("#district_name").eq(2).html();