Codeigniter上的Sweet Alert js

时间:2014-10-15 15:26:52

标签: javascript php jquery codeigniter sweetalert

有人使用过甜蜜的警报吗?我有一个问题。

这是我通过id_event删除帖子的代码:

 <a href="<?php echo base_url(); ?>admin/event/delete_event/<?php echo $row->id_event; ?>" title="delete"><i class="glyphicon glyphicon-trash" onClick="return(confirm('Are you sure?'));"></i></a> 

我想使用甜蜜警报js进行删除确认。这是代码:

swal({
  title: "Are you sure?",
  text: "Your will not be able to recover this post!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
},
function(){
  swal("Deleted!", "Your post has been deleted.", "success");
});

我想在点击confirmButton时删除帖子。 我该如何实现代码?谢谢你的帮助:)。

1 个答案:

答案 0 :(得分:1)

您需要使用JS来防止此链接上的默认操作并将AJAX请求发送到服务器。

这样的事情:

$('a[title=delete]').on('click', function(e) {
   e.preventDefault();
   $.ajax({
    url: 'admin/event/delete_event/'+$(@).attr('data-id'),
    method: "POST",
    success: function() {
     //code when success
     },
    error: function() {
     //code when error
    }
});

不幸的是,sweetAlert没有自己关闭模态的方法。所以我只是在关闭sweetAlert中返回closeModal方法。

如果您不想使用ajax,则无法理解删除是成功还是失败