2017-01-16 57 views
0

我的按钮删除有问题。 Picture我的按钮删除有点问题

如果我按第1行删除,警报将显示它。但是,如果我按下第2 - n行的按钮删除,它不会显示警报。

这是我的JS和HTML代码

\t \t \t <script type="text/javascript"> 
 
\t \t \t \t \t $('#AlertDelete').click(function(e) { 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t swal({ 
 
\t \t \t \t \t  title: "Yakin akan menghapus data?", 
 
\t \t \t \t \t  text: "Data yang sudah dihapus tidak akan bisa dikembalikan", 
 
\t \t \t \t \t  type: "warning", 
 
\t \t \t \t \t  showCancelButton: true, 
 
\t \t \t \t \t  confirmButtonColor: '#DD6B55', 
 
\t \t \t \t \t  confirmButtonText: 'Iya, hapus data!', 
 
\t \t \t \t \t  cancelButtonText: "Batalkan!", 
 
\t \t \t \t \t  confirmButtonClass: "btn-danger", 
 
\t \t \t \t \t  closeOnConfirm: false, 
 
\t \t \t \t \t  closeOnCancel: false 
 
\t \t \t \t \t  }, 
 

 
\t \t \t \t \t  function(isConfirm) { 
 
\t \t \t \t \t  if (isConfirm) { 
 
\t \t \t \t \t  \t var data = $(this).serialize(); 
 
\t \t \t \t \t \t \t $.post('<?php echo base_url();?>index.php/agen/delete/<?php echo $row['id_agen'];?>', data); 
 
\t \t \t \t \t  \t swal("Terhapus!", "Data berhasil dihapus. Kamu akan diarahkan ke halaman sebelumnya secara otomatis", "success"); 
 
\t \t \t \t \t \t \t setTimeout("location.href = 'http://localhost/cvatikan/index.php/buku';",1000); 
 
\t \t \t \t \t  } else { 
 
\t \t \t \t \t   swal("Dibatalkan", "Hapus data dibatalkan", "error"); 
 
\t \t \t \t \t  } 
 
\t \t \t \t \t  }); 
 
\t \t \t \t \t }); 
 
\t \t \t \t </script>
\t \t <h2 class="view-title">Kelola Agen</h2> 
 
\t \t <div id="masonry" class="row"> 
 
\t \t \t <div class="module-wrapper masonry-item col-lg-12 col-md-12 col-sm-12 col-xs-12"> 
 
\t \t \t \t <section class="module module-headings"> 
 
\t \t \t \t \t <div class="module-inner"> 
 
\t \t \t \t \t \t <div class="module-heading"> 
 
\t \t \t \t \t \t \t <h3 class="module-title">Daftar Agen</h3> 
 
\t \t \t \t \t \t \t <ul class="actions list-inline"> 
 
\t \t \t \t \t \t \t \t <li><a href="<?php echo base_url();?>index.php/Agen/manage"><span class="icon icon_plus"></span></a></li> 
 
\t \t \t \t \t \t \t \t <!--<li><button type="button" class="btn btn-xs btn-primary" data-dismiss="modal"><span class="icon icon_plus"></span></button></li>--> 
 
\t \t \t \t \t \t \t </ul> 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t <div class="module-content collapse in" id="content-1"> 
 
\t \t \t \t \t \t \t \t <div class="module-content-inner no-padding-bottom"> 
 
\t \t \t \t \t \t \t \t \t <div class="table-responsive"> 
 
\t \t \t \t \t \t \t \t \t \t <!-- di sini kontenny --> 
 
\t \t \t \t \t \t \t \t \t <table id="datatables-1" class="table table-striped display"> 
 
\t \t \t \t \t \t \t \t \t \t <thead> 
 
\t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <th>No</th> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <th>ID Agen</th> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <th>Nama Agen</th> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <th>Alamat</th> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <th>No. Telp</th> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <th>Keterangan</th> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <th>Action</th> 
 
\t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t </thead> 
 
\t \t \t \t \t \t \t \t \t \t <tbody> 
 
\t \t \t \t \t \t \t \t \t \t \t <?php if(@$result):?> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <?php $i = 1;?> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <?php foreach ($result as $row):?> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t <tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <td><?php echo $i++;?></td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <td><?php echo $row['id_agen'];?></td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <td><?php echo $row['nama_agen'];?></td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <td><?php echo $row['alamat'];?></td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <td><?php echo $row['no_telp'];?></td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <td><?php echo $row['keterangan'];?></td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <a href="<?php echo base_url();?>index.php/agen/manage/<?php echo $row['id_agen'];?>"class="btn btn-xs btn-primary"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t <a class="btn btn-xs btn-danger" id="AlertDelete"><i class="fa fa-trash"></i>Delete</a> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t </td> 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t </tr> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <?php endforeach?> 
 
\t \t \t \t \t \t \t \t \t \t \t <?php endif?> 
 
\t \t \t \t \t \t \t \t \t \t </tbody> 
 
\t \t \t \t \t \t \t \t \t </table> 
 
\t \t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </section> 
 
\t \t \t \t </div> 
 
\t \t \t </div>

我希望有人能帮助我解决这个问题。 Thx之前:)

回答

0

既然你动态地与您的UI数据绑定,你不必使用ID点击事件。而不是使用类名来调用你的点击事件。

只是要“AlertDelete”作为类名称如下图所示代码

<tbody> 
             <?php if(@$result):?> 
              <?php $i = 1;?> 
              <?php foreach ($result as $row):?> 
               <tr> 
                <td><?php echo $i++;?></td> 
                <td><?php echo $row['id_agen'];?></td> 
                <td><?php echo $row['nama_agen'];?></td> 
                <td><?php echo $row['alamat'];?></td> 
                <td><?php echo $row['no_telp'];?></td> 
                <td><?php echo $row['keterangan'];?></td> 
                <td> 
                <a href="<?php echo base_url();?>index.php/agen/manage/<?php echo $row['id_agen'];?>"class="btn btn-xs btn-primary"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a> 
                <a class="btn btn-xs btn-danger AlertDelete"><i class="fa fa-trash"></i>Delete</a> 
                </td> 
               </tr> 
              <?php endforeach?> 
             <?php endif?> 
            </tbody> 


And change below code in js 

    <script type="text/javascript"> 
         $(document). 
    on('click','.AlertDelete',function(e) { 

          swal({ 
           title: "Yakin akan menghapus data?", 
           text: "Data yang sudah dihapus tidak akan bisa dikembalikan", 
           type: "warning", 
           showCancelButton: true, 
           confirmButtonColor: '#DD6B55', 
           confirmButtonText: 'Iya, hapus data!', 
           cancelButtonText: "Batalkan!", 
           confirmButtonClass: "btn-danger", 
           closeOnConfirm: false, 
           closeOnCancel: false 
          }, 

          function(isConfirm) { 
           if (isConfirm) { 
           var data = $(this).serialize(); 
           $.post('<?php echo base_url();?>index.php/agen/delete/<?php echo $row['id_agen'];?>', data); 
           swal("Terhapus!", "Data berhasil dihapus. Kamu akan diarahkan ke halaman sebelumnya secara otomatis", "success"); 
           setTimeout("location.href = 'http://localhost/cvatikan/index.php/buku';",1000); 
           } else { 
           swal("Dibatalkan", "Hapus data dibatalkan", "error"); 
           } 
          }); 
         }); 
        </script> 
+0

您能否通过创建新问题并维护像这个问题的源代码来详细解释您的问题 – GSB

0

我不确定这是否是您的问题,但我会使用类而不是您的按钮的ID。最好不要使用同一个id的多个控件(它违反了W3C规范)。

欲了解更多信息,请参阅本post

+0

我试图将其更改为类和它的作品。多谢,伙计! :D –