2017-01-16 55 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 –