确认模态jquery引导程序

时间:2017-05-05 04:26:32

标签: jquery html

我有这段代码:

点击此链接:

   <td><?php echo anchor("atendimento/delete/{$record->id}", 'Excluir', ['class'=>'btn btn-danger', 
                            'name' => 'delete', 'id' => 'anchordelete']); ?></td>

显示包含两个按钮SimNao的模式,其中Sim表示是,Nao表示否。如果我点击Sim需要删除来自DB的数据。

<div class="modal fade" role="dialog" id="myModal">
   <div class="modal-dialog">
         <!-- Modal content-->
          <div class="modal-content" >
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Atenção</h4>
            </div>
            <div class="modal-body">
              <p id="confirm-message"></p>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-danger" id="confirm">Sim</button>
              <button type="button" class="btn btn-default" data-dismiss="modal" id="cancel">Não</button>
            </div>
          </div>
    </div>
  </div>

 jQuery(document).ready(function() {

    $("a[name='delete']").on('click', function(e){

        $("#confirm-message").html("Confirma a EXCLUSÃO do registro?");
        $('#myModal').modal("show");

        e.preventDefault();
    });

    $('#confirm').on('click', function(){

            $("a[name='delete']").trigger('click');
            $("a[name='delete']").removeAttr("name");

            $('#myModal').modal("hide");
    });

});

不工作,当我点击Nao时,模态关闭,但如果我点击Sim,则没有任何反复发生。

编辑:

这是我的php循环

 <?php if(count($records)){ ?>

                    <?php foreach($records as $record) {?>
                        <tr>
                          <td><?php echo $record->data; ?></td>
                          <td><?php echo $record->assunto; ?></td>
                          <td><?php echo $record->responsavel; ?></td>

                          <td><?php echo anchor("atendimento/edit/{$record->id}", 'Atualizar', ['class'=>'btn btn-success']); ?> </td>
                          <td><?php echo anchor("atendimento/delete/{$record->id}", 'Excluir', ['class'=>'btn btn-danger', 
                            'name' => 'delete', 'id' => 'anchordelete']); ?></td>

                        </tr>
                    <?php }?>
               <?php } ?>

2 个答案:

答案 0 :(得分:0)

使用:

$("#anchordelete")[0].click();

jQuery的.click()将尝试添加点击处理程序,而不是实际点击该元素。使用[0]将引用第一个元素,然后单击它。

答案 1 :(得分:-1)

使用window.location转到删除页面,使用数据属性将点击后的网址传递给模态

jQuery(document).ready(function() {

    $("a[name='delete']").on('click', function(e){

        $("#confirm-message").html("Confirma a EXCLUSÃO do registro?");
        $('#myModal').attr('data-url',$(this).attr('href'));
        $('#myModal').modal("show");

        e.preventDefault();
    });

    $('#confirm').on('click', function(){
          window.location = $(this).closest('#myModal').attr('data-url')
    });

});