weixin_33713503 2016-06-25 17:55 采纳率: 0%
浏览 4

AJAX JQuery删除数据

Help, I have a big problem with ajax, I deleted records using ajax, using confirmation window bootstrap, very well when I press the button to delete it runs correctly and the table refreshes without any problem, the problem is that when I want to remove another record, opens the modal window bootstrap, but do not run anything

<head>
        <script type="text/javascript" language="Javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
        <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
        <script data-require="bootstrap@*" data-semver="3.1.1" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        <link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
            <!--<script type="text/javascript" language="Javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>-->
    </head>
    <script type="text/javascript" >
        var base_url = '<?php echo base_url() ?>clientes/eliminar';
            $('#confirmar').click(function (e) {
                var modalDiv = $(e.delegateTarget);
                var id = $(this).data('recordId');
                $('#confirm-delete').addClass('loading');
                setTimeout(function () {
                    modalDiv.modal('hide').removeClass('loading');
                }, 100000);
                $.ajax({
                    type: "POST",
                    url: base_url,
                    data: "idcliente=" + id,
                    beforeSend: function () {
                        //$('#msgUsuario').html('<span></span>');
                    },
                    success: function (respuesta) {
                        $('#confirm-delete').hide();
                        $('.modal-backdrop').hide();
                        $('#contenedor2').html(respuesta);
                    }
                });

            });

            // Bind to modal opening to set necessary data properties to be used to make request
            $('#confirm-delete').on('show.bs.modal', function (e) {

                var data = $(e.relatedTarget).data();
                $('.title', this).text(data.recordTitle);
                $('.btn-ok', this).data('recordId', data.recordId);
            });
        });

    </script>
    <style>
        .modal.loading .modal-content:before {
            content: 'Loading...';
            text-align: center;
            line-height: 155px;
            font-size: 20px;
            background: rgba(0, 0, 0, .8);
            position: absolute;
            top: 55px;
            bottom: 0;
            left: 0;
            right: 0;
            color: #EEE;
            z-index: 1000;
        }
    </style>
    <li><a data-toggle="modal" data-target="#events-modal">Nuevo Cliente</a></li>
<div id="contenedor2" class="col-md-8 col-md-offset-2">
    <table >
        <tr>
            <th class="col-xs-4" style="text-align:left;border:1px solid">Name</th>
            <th class="col-xs-4" style="text-align:left;border:1px solid">tel</th>
            <th class="col-xs-2" style="text-align:center;border:1px solid">Email</th>
            <th class="col-xs-1" style="text-align:center;border:1px solid">Edit</th>
            <th class="col-xs-1" style="text-align:center;border:1px solid">Delete</th>
        </tr>
        <tbody class="tablenotas">
            <?php
            if (is_null($lista)) {
                echo "<h4>Para comenzar presiona el bot&oacute;n Nuevo</h4>";
            } else {
                $base = base_url();
                foreach ($lista as $fila):
                    echo '<tr style="text-align:justify">' .
                    '<td style="text-align:left;border:1px solid">' . $fila->NombreCliente . '</td>' .
                    '<td style="text-align:left;border:1px solid">' . $fila->RazonSocial . '</td>' .
                    '<td style="text-align:center;border:1px solid">' . $fila->Email . '</td>' .
                    '<td style="text-align:center;border:1px solid"><a  href="' . $base . 'clientes/detalles_cliente/' . $fila->Id_Cliente . '"><span class="glyphicon glyphicon-edit"></span></a></td>' .
                    '<td style="text-align:center;border:1px solid"><a href="#" data-record-id="' . $fila->Id_Cliente . '"  data-toggle="modal" data-target="#confirm-delete"><span class="glyphicon glyphicon-trash"></span></td>' .
                    '</tr>';
                endforeach;
            }
            ?>
        </tbody>
    </table>
    <div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
                </div>
                <div class="modal-body">
                    <p>Estás a punto de eliminar <b><i class="title"></i></b> el registro, este procedimiento es irreversible.</p>
                    <p>Desea proceder?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                    <button type="button" id="confirmar" class="btn btn-danger btn-ok">Continuar</button>
                </div>
            </div>
        </div>
    </div>
</div>

in my controller

function delete() {
    $idcliente = $this->input->post('idcliente');
    if ($this->ion_auth->is_admin()) {
        if ($this->Cliente_model->delete($idcliente)) {
            $this->_data['lista'] = $this->Cliente_model->getClientes();
            $this->load->view('user/clientes', $this->_data);
        }
    }
}
  • 写回答

1条回答 默认 最新

  • lrony* 2016-06-25 19:05
    关注

    It look like that the modal html is inside the DIV with id #contenedor2 so when you change the HTML ($('#contenedor2').html(respuesta);) you also add a new DIV with the modal HTML, BUT this will not work after that:

    $('#confirmar').click(function (e) {
        // YOUR CODE
    });
    

    To fix it, try delegating the event like this (replace the above code with the following):

    $('#contenedor2').on('click', '#confirmar', function (e) {
        e.preventDefault();
    
        var modalDiv = $(e.delegateTarget);
        var id = $(this).data('recordId');
        ..... REST OF YOUR CODE
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题