【问题标题】:Ajax call delete cakephpAjax 调用删除 cakephp
【发布时间】:2013-07-24 00:25:47
【问题描述】:

我正在尝试使用 ajax delete 删除记录,单击时会在发送请求之前确认。 记录被删除并且它可以工作,但问题是在删除视图中的任何更改之后,直到我手动重新加载页面之后。 我想在对话框中单击“确定”后在视图中显示结果

我的 ajax 代码:

$(document).ready(function() { 
  if($('.confirm_delete').length) {
       $('.confirm_delete').click(function(){
         var result = confirm('Are you sure you want to delete this?');
         $('#flashMessage').fadeOut();
         if(result) {
           $.ajax({
              type:"POST",
              url:$(this).attr('href'),
              data:"ajax=1",
              dataType: "json",
              success:function(response){
                }
          });
      }

      return false;
    });
  }
});

在视图中:

echo $this->Js->link('Delete', array('controller' => 'publications', 'action'=>'delete', $publication['Publication']['id']),
  array('escape' => false, 'class'=>'confirm_delete'));

【问题讨论】:

    标签: html ajax cakephp delete-record


    【解决方案1】:
    $(document).ready(function(){
    if($('.confirm_delete').length) {
       $id=$(this).attr('id');
    
       $('.confirm_delete').click(function(){
       var result = confirm('Are you sure you want to delete this?');
        $('#flashMessage').fadeOut();
    
        if(result) {
            $.ajax({
                type:"POST",
                url:$(this).attr('href'),
                data:"ajax=1",
                dataType: "json",
                success:function(response){
    
                    }
            });
    
    
            $('#row'+$id).remove();
    
    }
    
    
    return false;
    });
    

    } });

    由于某种原因 $(this).attr('id') 不起作用...如何获取所选元素的 id 以将其删除 我有我的看法:

    <div class="box_detail" id="row<?php echo $publication['Publication']['id']; ?>">
    

    【讨论】:

      【解决方案2】:

      这不是 CakePHP 的问题,而只是 JS 的问题。如果您的删除回调成功且没有返回任何错误,您必须从 DOM 树中删除相关内容。使用 jquery 可以通过在要删除的任何选择器上调用 remove() 来完成。

      【讨论】:

      • 谢谢你的回答.. 但你能告诉我我是怎么做的吗:success:function(response){ $('.liste_tab').remove();在我看来:
        但没有任何改变 }
      • @JOHN 奇怪,应该这样做。尝试用 id 替换你的类,或者只是使用 js 重新加载页面,比如 window.location.reload(true);或 window.setTimeout('location.reload()', 3000); //在你的成功函数中 3 秒后重新加载
      • 如果我把删除成功函数没有任何改变,但如果我把它放在(ajax 调用)之后,它会删除但它显示我的 div 为空我想显示所有记录并隐藏删除的记录。 . 我的意思是更新 div 这是代码: if(result) { $.ajax({ type:"POST", url:$(this).attr('href'), data:"ajax=1", dataType: “json”,成功:函数(响应){}}); $('.box_detail').remove(); }
      • 我想在不重新加载页面的情况下修复它,有什么帮助吗?
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签