dongzhuo2371 2015-11-08 05:01
浏览 56
已采纳

带有Codeigniter的Sweet Alert用于删除事件

I'm having problems trying to make sweet alerts work on my codeigniter code, on the index of my clients module i have (part of code):

<table class="table table-striped">
    <tr>
        <td>ID</td>
        <td>Empresa</td>
        <td>Contacto</td>
        <td>RNC</td>
        <td>Telefono</td>
        <td>Email</td>
        <td>Acciones</td>
    </tr>
    <?php foreach($clientes as $c): ?>
    <tr>
        <td><?php echo $c['id']; ?></td>
        <td><?php echo $c['empresa']; ?></td>
        <td><?php echo $c['contacto']; ?></td>
        <td><?php echo $c['rnc']; ?></td>
        <td><?php echo $c['telefono']; ?></td>
        <td><?php echo $c['email']; ?></td>
        <td>
            <a href="<?php echo site_url('clientes/edit/'.$c['id']); ?>" class="btn btn-info">Editar</a> 
            <a href="<?php echo site_url('clientes/delete/'.$c['id']); ?>" class="btn btn-danger">Eliminar</a>
            <a href="<?php echo site_url('clientes/view/'.$c['id']); ?>" class="btn btn-danger">Ver</a>

        </td>
    </tr>
    <?php endforeach; ?>

</table>

Then i have in my js this:

$('#AlertaEliminarCliente').on("click", function() {
  swal({
      title: "Está seguro?",
      text: "No podrá recuperar el cliente una vez sea eliminado!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: '#DD6B55',
      confirmButtonText: 'Si, Eliminarlo!',
      cancelButtonText: "No, Cancelar!",
      confirmButtonClass: "btn-danger",
      closeOnConfirm: false,
      closeOnCancel: false
    },
    function(isConfirm) {
      if (isConfirm) {
        swal("Eliminado!", "Su cliente ha sido eliminado!", "success");
      } else {
        swal("Cancelado", "Su cliente está a salvo! :)", "error");
      }
    });
});

How can i add the id #AlertaEliminarCliente to my button

i have tried to add it and it just delete the row without asking for confirmation etc.. from sweet alert, is there something that i am missing? any guidance its really appreciated, thanks guy for your time!

</div>
  • 写回答

2条回答 默认 最新

  • douchuo9476 2015-11-08 05:52
    关注

    Clicking on the button will take the user to the url clientes/delete/'.$c['id'] regardless of user interaction with sweet alert. You need to modify your jQuery to prevent the url change, and only redirect the user on their choice of the sweet alert prompt.

    $('#AlertaEliminarCliente').on("click", function(e) {
      e.preventDefault();
      var url = $(this).attr('href');
      swal({
          title: "Está seguro?",
          text: "No podrá recuperar el cliente una vez sea eliminado!",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: '#DD6B55',
          confirmButtonText: 'Si, Eliminarlo!',
          cancelButtonText: "No, Cancelar!",
          confirmButtonClass: "btn-danger",
          closeOnConfirm: false,
          closeOnCancel: false
        },
        function(isConfirm) {
          if (isConfirm) {
            swal("Eliminado!", "Su cliente ha sido eliminado!", "success");
            window.location.replace(url);
          } else {
            swal("Cancelado", "Su cliente está a salvo! :)", "error");
          }
        });
    });
    

    You may want to set a timeout to still display the final message before the url change

    setTimeout(function(){ window.location.replace = url; }, 2000);
    

    Final note: if you intend to use this method on more than one element on the page you should use .AlertaEliminarCliente rather than #AlertaEliminarCliente

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c