【问题标题】:How to get the id of a record in a html table?如何获取html表中记录的id?
【发布时间】:2015-10-10 04:52:12
【问题描述】:

我有一个包含我的数据库的一些记录的表。我正在尝试使用 JQuery 获取这些寄存器的 id,但我不知道该怎么做。

我该怎么做?

<script type="text/javascript">
    $(document).ready(function(){        
        $('#btnAbrirEmpresa').click(function(){ 
            //id register
            var id = $(this).closest('tr').attr('[Empresa][id]');
            alert(id);

            var loading = $(".imageLoading");
            $(document).ajaxStart(function () {
                loading.show();
            });
            $(document).ajaxStop(function () {
                loading.hide();
            });

            $.ajax({
                accepts: {json: 'application/json'},
                dataType:'json',
                type: "POST",
                url: "<?php echo $this->Html->url("/Empresas/openEmpresa.json")?>",
                data: {"id":id},
                success: function( data ){ 
                    console.log(data);                                 
                },
                error: function (xhr, status) {                    
                    $(".message").html('Error: ' + status);
                }   
            });

            return false;
        });
    });
</script>


<div class="row">   
        <div class="col-lg-12">
            <h1 class="page-header">Empresa</h1> 

<!--loading-->
<div class="imageLoading" style="display: none">
    <?php echo $this->Html->image("ajax-loader.gif", array("height"=>"32", "width"=>"32"));?>     
</div>
<!--/loading-->

            <span><?php echo $this->Html->link(__('Novo'), array('action' => 'add')); ?></span>
            <div class="panel panel-default">                
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="dataTable_wrapper">
                        <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                            <thead>
                                <th><?php echo $this->Paginator->sort('id'); ?></th>
                                <th><?php echo $this->Paginator->sort('nomeFantasia'); ?></th>                                
                                <th><?php echo $this->Paginator->sort('cnpj'); ?></th>
                                <th><?php echo $this->Paginator->sort('telefone1'); ?></th>
                                <th><?php echo $this->Paginator->sort('telefone2'); ?></th>
                                <th><?php echo $this->Paginator->sort('celular'); ?></th>
                                <th><?php echo $this->Paginator->sort('aberto'); ?></th>

                                <th class="actions"><?php echo __('Actions'); ?></th>
                            </thead>
                            <tbody>
                                <?php foreach ($empresas as $empresa): ?>
                                        <tr>
                                                <td><?php echo h($empresa['Empresa']['id']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['nomeFantasia']); ?>&nbsp;</td>                                                
                                                <td><?php echo h($empresa['Empresa']['cnpj']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['telefone1']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['telefone2']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['celular']); ?>&nbsp;</td>                                                
                                                <td><?php echo h($empresa['Empresa']['aberto'] == 1 ? "Sim" : "Não"); ?>&nbsp;</td>

                                                <td class="actions">                                                    
                                                       <?php echo $this->Html->link('<i class="glyphicon glyphicon-open"></i>',
                                                                    array(),
                                                                    array("id"=>"btnAbrirEmpresa", 'title'=>'abrir empresa', 'escape' => false)); ?> 
                                                </td>
                                        </tr>
                                <?php endforeach; ?>                                       
                            </tbody>
                        </table>
                        <p>
                            <?php
                            echo $this->Paginator->counter(array(
                                    'format' => __('Página {:page} de {:pages}, exibindo {:current} registro de {:count}, início {:start}, final {:end}')
                            ));
                            ?>  </p>
                            <div class="paging">
                            <?php
                                    echo $this->Paginator->prev('< ' . __(' previous '), array(), null, array('class' => 'prev disabled'));
                                    echo $this->Paginator->numbers(array('separator' => ''));
                                    echo $this->Paginator->next(__(' next ') . ' >', array(), null, array('class' => 'next disabled'));
                            ?>
                    </div>                            
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->
    </div>
</div>

【问题讨论】:

    标签: jquery cakephp-2.0


    【解决方案1】:

    解决了这个问题。我在 $this-&gt;Html-&gt;link 的 id 中添加了注册 id 并且工作正常。

    解决方案

    <script type="text/javascript">
    
    $(document).on('click', '.btnAbrirEmpresa', function(e){
        e.preventDefault(); 
        var pai = $(this).closest('tr'); 
        var id = this.id; 
    
        var loading = $(".imageLoading");
        $(document).ajaxStart(function () {
            loading.show();
        });
        $(document).ajaxStop(function () {
            loading.hide();
        });
    
        $.ajax({
            accepts: {json: 'application/json'},
            dataType:'json',
            type: "POST",
            url: "<?php echo $this->Html->url("/Empresas/openEmpresa.json")?>",
            data: {id:id},
            success: function( data ){ 
                console.log(data);
    
            },
            error: function (xhr, status) {                    
                $(".message").html('Error: ' + status);
            }   
        });
    
        return false;    
    });
    
    </script>
    
    
    <div class="row">   
            <div class="col-lg-12">
                <h1 class="page-header">Empresa</h1> 
    
    <!--loading-->
    <div class="imageLoading" style="display: none">
        <?php echo $this->Html->image("ajax-loader.gif", array("height"=>"32", "width"=>"32"));?>     
    </div>
    <!--/loading-->
    
                <span><?php echo $this->Html->link(__('Novo'), array('action' => 'add')); ?></span>
                <div class="panel panel-default">                
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
                                    <th><?php echo $this->Paginator->sort('id'); ?></th>
                                    <th><?php echo $this->Paginator->sort('nomeFantasia'); ?></th>                                
                                    <th><?php echo $this->Paginator->sort('cnpj'); ?></th>
                                    <th><?php echo $this->Paginator->sort('telefone1'); ?></th>
                                    <th><?php echo $this->Paginator->sort('telefone2'); ?></th>
                                    <th><?php echo $this->Paginator->sort('celular'); ?></th>
                                    <th><?php echo $this->Paginator->sort('aberto'); ?></th>
    
                                    <th class="actions"><?php echo __('Actions'); ?></th>
                                </thead>
                                <tbody>
                                    <?php foreach ($empresas as $empresa): ?>
                                            <tr>
                                                    <td><?php echo h($empresa['Empresa']['id']); ?>&nbsp;</td>
                                                    <td><?php echo h($empresa['Empresa']['nomeFantasia']); ?>&nbsp;</td>                                                
                                                    <td><?php echo h($empresa['Empresa']['cnpj']); ?>&nbsp;</td>
                                                    <td><?php echo h($empresa['Empresa']['telefone1']); ?>&nbsp;</td>
                                                    <td><?php echo h($empresa['Empresa']['telefone2']); ?>&nbsp;</td>
                                                    <td><?php echo h($empresa['Empresa']['celular']); ?>&nbsp;</td>                                                
                                                    <td><?php echo h($empresa['Empresa']['aberto'] == 1 ? "Sim" : "Não"); ?>&nbsp;</td>
    
                                                    <td class="actions">                                                    
    
                                                        <?php echo $this->Html->link('<i class="glyphicon glyphicon-open"></i>',
                                                                        array(),
                                                                        array("id"=>$empresa['Empresa']['id'],
                                                                            'class'=>'btnAbrirEmpresa',
                                                                            'title'=>'abrir empresa', 
                                                                            'escape' => false)); ?> 
                                                    </td>
                                            </tr>
                                    <?php endforeach; ?>                                       
                                </tbody>
                            </table>
                            <p>
                                <?php
                                echo $this->Paginator->counter(array(
                                        'format' => __('Página {:page} de {:pages}, exibindo {:current} registro de {:count}, início {:start}, final {:end}')
                                ));
                                ?>  </p>
                                <div class="paging">
                                <?php
                                        echo $this->Paginator->prev('< ' . __(' previous '), array(), null, array('class' => 'prev disabled'));
                                        echo $this->Paginator->numbers(array('separator' => ''));
                                        echo $this->Paginator->next(__(' next ') . ' >', array(), null, array('class' => 'next disabled'));
                                ?>
                        </div>                            
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-06
      • 2016-10-02
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      相关资源
      最近更新 更多